UITableView 编辑

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

NSArray *array1 = [NSArray arrayWithObjects:@"美国", @"法国", nil];

NSArray *array2 = [NSArray arrayWithObjects:@"中国", @"韩国", nil];

NSArray *array3 = [NSArray arrayWithObjects:@"日本", @"朝鲜", @"阿联酋", nil];

self.arr = [NSMutableArray arrayWithObjects:array1, array2, array3, nil];

self.TBV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.bounds.size.height - 14) style:UITableViewStyleGrouped];

self.TBV.delegate = self;

self.TBV.dataSource = self;

[self.view addSubview:self.TBV];

[self.TBV release];

//设置一个右边的编辑按钮(系统已经初始化好的)

self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{

[super setEditing:editing animated:animated];

[self.TBV setEditing:editing animated:animated];

}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.section == 2) {

return UITableViewCellEditingStyleInsert;

}

return UITableViewCellEditingStyleDelete;

}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!编辑的实现

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

//数据操作

NSMutableArray *array = [NSMutableArray arrayWithObject:[_arr  objectAtIndex:indexPath.section]];

//数据删除

[self.arr removeObjectAtIndex:indexPath.row];

[_arr replaceObjectAtIndex:indexPath.section withObject:array];

//cell 的操作

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

}

if (editingStyle == UITableViewCellEditingStyleInsert) {

}

}

////数据操作

//NSMutableArray *array = [NSMutableArray arrayWithObject:[_arr  objectAtIndex:indexPath.section]];

////数据删除

//[self.arr removeObjectAtIndex:indexPath.row];

//[_arr replaceObjectAtIndex:indexPath.section withObject:array];

//

////cell 的操作

//[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];

//}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    NSLog(@"编辑开始");

//    // 先判断当前的编辑模式

//    if (editingStyle == UITableViewCellEditingStyleDelete) {

//        // 先把数组里的对象删除掉

//        [self.arr removeObjectAtIndex:indexPath.row];

//        // 第一种方式

//        //        [self.tableView reloadData];

//        // 第二种方式

//        [self.TBV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

//    }

//}

//有几个section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return [_arr count];

}

//每个section上有几行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [[_arr objectAtIndex:section] count];

}

//每个section上的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//重用标识

static NSString *reuse = @"reuse";

//从重用队列里面取

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];

//若没有,初始化

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];

cell.textLabel.text = [[_arr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

}

return cell;

}

你可能感兴趣的:(UITableView 编辑)