IOS-tableview编辑

今天博主有一个tableview编辑的需求,遇到了一些困难点,在此和大家分享,能够共同进步.

tableview的编辑是通过[self.tableview setEditing: BOOL1 animotion: BOOL2];进入的,如果需要进入编辑模式,则调用方法,将BOOL1改为YES.如果要退出编辑模式,则调用方法,将BOOL1改为NO.BOOL2为是否使用动画.

如果你想将编辑这个button由文字改为图片,想通过setBackGroudImage这个方法替换文字,你会发现图片会因为无法设置frame而产生拉伸,效果不理想.解决方法:你可以自定义rightBarButtonItem,重写代理方法.下面将tableview的编辑的代码贴出来,与大家分享

- (void)viewDidLoad {

    [super viewDidLoad];

 

    //编辑按钮

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.navigationItem.rightBarButtonItem.title = @"编辑";

    self.navigationItem.rightBarButtonItem.tintColor=[UIColor blackColor];

}

#pragma mark---------tableView的编辑(删除,插入)

//点击编辑按钮

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

    [super setEditing:editing animated:animated];

    // Don't show the Back button while editing.

//    [self.navigationItem setHidesBackButton:editing animated:YES];

    if (editing) {

        self.navigationItem.rightBarButtonItem.title = @"完成";

        NSLog(@"abc");

    }else {//点击完成按钮

        self.navigationItem.rightBarButtonItem.title = @"编辑";

        NSLog(@"123");

    }

    [_customView.tableView setEditing:editing animated:YES];

}

//2.设置定制分区(section)中的行(row)是否可以被编辑

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

{

//    if (indexPath.section==0) {

//        return NO;

//    }

    return YES;

}

//3.设置指定的分区(section)的行(row)编辑的样式(添加,删除)

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

{

    if (indexPath.section==1) {

        return UITableViewCellEditingStyleInsert;

    }

    return UITableViewCellEditingStyleDelete;

}

//4.编辑完成(先操作数据源,再修改UI)

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

{

    if (editingStyle==UITableViewCellEditingStyleDelete) {

        NSLog(@"删除");

        //同步更新beginUpdates 和 endUpdates 之间修改的UI和数据

        [_customView.tableView beginUpdates];

        

        //1.删除数据

        NSString *key=_allDataDic.allKeys[indexPath.section];

        NSMutableArray *array=_allDataDic[key];

        [array removeObjectAtIndex:indexPath.row];

        //2.删除UI

        //删除行

        [_customView.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        if (array.count==0) {

            

            //删除区头上的数据(删除key对应的小数组)

            NSString *key=_allDataDic.allKeys[indexPath.section];

            [_allDataDic removeObjectForKey:key];

 

            //当某个分区中只剩一个cell的时候(或者小数组中只剩一个数据的时候),删除时,也要把区头删除掉

            NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:indexPath.section];

            [_customView.tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationLeft];

        }

        

        [_customView.tableView endUpdates];

        

    }else if (editingStyle==UITableViewCellEditingStyleInsert)

    {

        NSLog(@"插入");

        //1.插入数据

        NSString *key=_allDataDic.allKeys[indexPath.section];

        NSMutableArray *array=_allDataDic[key];

        [array insertObject:@"昌平" atIndex:indexPath.row];

        //2.插入UI(cell)        

      //  NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:0 inSection:indexPath.section];//保证每次插入数据都在第一行

        [_customView.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];        

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值