UITableViewCell 的自定义多选删除

作者: TotoroLee | 来源:发表于2016-04-27 10:42 被阅读2875次

点击编辑按钮,使UITableView处于编辑状态,UITableViewCell可以选中取消,选中时自定义选中图片.之后点击删除按钮,删除选中状态下的Cell.如图:

解释下主要的代码:

1.UITableView 是否可以进入编辑状态,可根据indexPath设置需要编辑的section和row.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

2.@property (nonatomic, getter=isEditing) BOOL editing; 打开关闭编辑模式.

- (void)editingCilck:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        [self.view addSubview:self.editView];
        
        //启动表格的编辑模式
        self.deleteTableView.editing = YES;
    }else{
        [sender setTitle:@"编辑" forState:UIControlStateNormal]; 
        [self.editView removeFromSuperview];
        
        //关闭表格的编辑模式
        self.deleteTableView.editing = NO;
    }
}

3.设置编辑样式(如果不设置,会使用系统自带的删除方法,不能多选删除,只能单个删除. 会调用注释掉的那个方法)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

// 系统自带的单个删除的方法
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self.dataArray removeObjectAtIndex:indexPath.row];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//    }
//}

4.取出系统自带的选中视图

UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];

解释: 在TableView允许编辑状态下点击Cell, 打印 cell.subviews,其中UITableViewCellEditControl 即为编辑状态下的视图, [[cell.subviews lastObject]subviews] 选中状态下的视图.

UITableView处于未编辑状态下.png UITableView处于编辑状态下.png

5.解决Cell在复用时,自定义的选中按钮不会被系统还原.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 此处要使用带 forIndexPath: 的这个方法.否则自定义选中视图,在Cell的复用中会变回系统自带的按钮图片.
    DeleteViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeleteViewCell" forIndexPath:indexPath];
    cell.deleteLable.text = self.dataArray[indexPath.row];

      //此处一定不能使用 UITableViewCellSelectionStyleNone, 会造成选中之后再次点击无法改变选中视图的样式(会有很多问题,可以试试哈)
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;

      //复用过程中,自定义的选中按钮不会被系统还原,需配合上述 forIndexPath: 方法(需在 Cell选中状态 和 TableView编辑状态下)
    if (cell.selected == YES && tableView.editing == YES) {
        UIImageView *imageView = [[[cell.subviews lastObject]subviews]firstObject];
        imageView.image = [UIImage imageNamed:@"chose_06"];
    }
    return cell;
}

6.用于监听删除数据中数组的个数

[RACObserve(self, number) subscribeNext:^(id x) {

}];
代码在这里-- Demo

END.

相关文章

网友评论

  • 634585edc862:请问楼主,我现在点击cell会push到另一个界面,选择删除的时候直接push了,这个怎么解决啊
    TotoroLee:@子瓜虫 点击Cell的方法中 根据tableView是否处于编辑状态做判断, tableView.editing == YES 时选择删除, NO 的时候做跳转.
  • 简书白兔:同楼上的问题,偶尔点击 还是会呈现系统的蓝色
  • _YZG_:有个问题, 就是一直按着cell的时候会是系统的蓝色
    TotoroLee:@_YZG_ 在继承于UITableViewCell的类里实现方法- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated,并重写选中状态下的视图. Demo已上传.
    _YZG_:@TotoroLee 不滑动,长按就行
    TotoroLee:@_YZG_ 恩, 是的.滑动的时候点击的那个Cell也会有短暂的蓝色,暂时还没有找到解决的办法.
  • VoiderSun:能不能自定义一个拖拽滑动cell
    TotoroLee:@VoiderSun 自定义拖动的那个按钮 ??
    VoiderSun:@TotoroLee 自定义拖拽一个的Cell到其他位置
    TotoroLee:@VoiderSun 拖拽滑动?? 你是指单独滑动一个的Cell删除,还是那种可以拖拽一个的Cell到其他位置??
  • Adrift:get
    TotoroLee:@Adrift :blush: :blush: :blush: :blush: :blush:

本文标题:UITableViewCell 的自定义多选删除

本文链接:https://www.haomeiwen.com/subject/qdrcrttx.html