UITableView详解(UITableViewCell(四) 增加 删除 移动)

来源:互联网 发布:csgo耳机推荐知乎 编辑:程序博客网 时间:2024/05/16 01:18
@implementation HMTAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];        HMTRootViewController * rootRC = [[HMTRootViewController alloc]init];        UINavigationController * rootNC = [[UINavigationController alloc]initWithRootViewController:rootRC];        self.window.rootViewController = rootNC;            [self.window makeKeyAndVisible];    return YES;}#import <UIKit/UIKit.h>@interface HMTRootViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{    int i ;}@property (nonatomic,retain)NSMutableArray * iPhoneArrays;@property (nonatomic,retain)UITableView *  iPhoneTableView;@end#import "HMTRootViewController.h"@interface HMTRootViewController ()@end@implementation HMTRootViewController-(void)dealloc{    RELEASE_SAFELY(_allGroupArray);    RELEASE_SAFELY(_iPhoneTableView);    [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization        _iPhoneArrays = [[NSMutableArray alloc]init];        i = 0;            }    return self;}- (void)viewDidLoad{    [super viewDidLoad];        // 灵活运用数组    // 一个区的每一行数据(小数组)    NSArray * array1 = [NSArray arrayWithObjects:@"中国",@"日本",@"韩国", nil];    NSArray * array2 = [NSArray arrayWithObjects:@"德国",@"英国",@"法国",@"瑞士", nil];    NSArray * array3 = [NSArray arrayWithObjects:@"美国",@"俄罗斯",@"墨西哥", nil];    // 每一个区的数据(大数组)    self.iPhoneArrays = [NSMutableArray arrayWithObjects:array1,array2,array3, nil];        // 建立一个表单视图    self.iPhoneTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 44)                       style:UITableViewStyleGrouped];        _iPhoneTableView.separatorColor = [UIColor redColor];    _iPhoneTableView.delegate = self;    _iPhoneTableView.dataSource = self;        [self.view addSubview:_iPhoneTableView];        /*    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit                                    target:self action:@selector(didClickRightBarButtonItemAction:)];    self.navigationItem.rightBarButtonItem = rightButton;    */    // 用系统定义好的一个edit按钮    self.navigationItem.rightBarButtonItem = self.editButtonItem;    self.editButtonItem.title = @"编辑";    // Do any additional setup after loading the view.}#pragma mark - 设置编辑环境,开启/关闭// 系统自带按钮- (void)setEditing:(BOOL)editing animated:(BOOL)animated{        [super setEditing:editing animated:animated];        #pragma mark 推荐这种写法    self.editButtonItem.title = editing?@"完成":@"编辑";        [_listTableView setEditing:editing animated:animated];    NSLOG_FUNCTION;    }// 自定义编辑按钮- (void)didClickRightBarButtonItemAction:(UIBarButtonItem *)rightButton{        if (_isEditing) {        _isEditing = NO;        rightButton.title = @"编辑";    } else {        _isEditing = YES;        rightButton.title = @"完成";    }    // 熟悉这种写法    //_isEditing = !_isEditing;    [_listTableView setEditing:_isEditing animated:YES];    }#pragma mark - 数据设置// 设置分区- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return [_iPhoneArrays count];    }// 设置每个分区的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [[_iPhoneArrays objectAtIndex:section] count];}// cell的具体实现和设置- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString * celldentifier = @"cell";    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:celldentifier];        if (!cell) {         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celldentifier];        i++;NSLog(@" ------%i---------",i);    }        cell.textLabel.text = [[_iPhoneArrays objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];            return cell;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    if (section == 0) {        return @"亚洲";    } else if (section == 1){        return @"欧洲";    }else        return @"北美洲";}// cell被选中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{        // 取消选中后显示的高亮状态,也就是只在选中的那一刻出现高亮    [tableView deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark - 数据编辑Edit 删除/添加#pragma mark 询问具体到某一区某一行能否被编辑(删除/添加)// 询问能否被编辑 ----- 系统默认全部cell能编辑,这个方法能够让你设置具体到哪一行哪一列- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}#pragma mark 返回编辑样式,是添加还是删除- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{        // 第3个分区进行增加操作    if (indexPath.section == 2) {        return  UITableViewCellEditingStyleInsert;    }        // 其余分区进行删除操作    return UITableViewCellEditingStyleDelete;}#pragma mark 最后编辑操作(单纯的设置这个方法,cell向左滑动会出现delete操作,短信.微信.QQ都是此操作)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{        // 判断状态    // 删除数据    if (editingStyle == UITableViewCellEditingStyleDelete) {            // 做判断,如果        if ([deletaArray count] > 1) {                        // 先删除数据            [[_iPhoneArrays objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];            // 删除cell(指定行)            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section], nil]                                               withRowAnimation:UITableViewRowAnimationLeft];            //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];                    }else{                    // 直接移除掉整个数组            [_iPhoneArrays removeObjectAtIndex:indexPath.section];            // 删除整个分区            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];                    }            }        // 添加数据    if (editingStyle == UITableViewCellEditingStyleInsert) {                NSString * text = @"天空之城";        // 先添加数据源        [[_iPhoneArrays objectAtIndex:indexPath.section] addObject:addCellText];        // 自定义要插入的行数(indexPath.row + ?(?>0) ?是几就代表添加的数据会出现在+号下面的几个位置处 ?为0就会出现在点击加号那一行的上面)        NSIndexPath * selfIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:selfIndexPath] withRowAnimation:UITableViewRowAnimationLeft];                    }}#pragma mark - 数据编辑Edit 移动#pragma mark 询问具体到某一区某一行能否被编辑(移动)// 询问能否被移动(参数indexPath作用:指点哪些区哪些行能够移动或者不能够移动)- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{    return YES;}#pragma mark 设置能否被移动- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{    // 一般,移动都是发生在同区之间,数据在不同区之间移动,那么数据一开始就应该不会编辑到一个区    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {        // 允许移动(目的地)        return proposedDestinationIndexPath;            }else{        // 不允许移动(源始地)        return sourceIndexPath;        }    }#pragma mark 完成最后移动,主要操作数据就行(注:移动并不是交换,移动看看就知道了,移动到一个位置,原先所有的cell会自动向下)- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{        // 取得要移动的数据    NSString * moveString = [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];    // 移动到目的地    [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] insertObject:moveString atIndex:destinationIndexPath.row];    // 删除源始地的数据    [[_iPhoneArrays objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

0 0