iOS 多选删除(附tableViewTips及单选删除)

1、前言

此次分享并记录一下tableView的多选删除,并额外记录一下cell的设置小技巧。数组

2、想要实现的效果图以下:

一、先上原图ide

二、而后编辑图以下字体

三、编辑步骤:ui

**- 点击右上角按钮编辑,界面呈现编辑状态底部删除按钮弹出spa

  • 选择删除cell项,点击右下角删除可删除
  • 点击右上角,退出编辑状态,底部删除按钮退出界面**

3、多选删除核心代码

一、设置容许tableView编辑状态下容许多选 代理

_mainTableView.allowsMultipleSelectionDuringEditing = YES;复制代码

二、将cell设置为可选择的风格(下面代码是在自定义Cell内部)code

self.selectionStyle = UITableViewCellSelectionStyleDefault;复制代码

说明:由于自认为系统的选中状态很难看,因此在cell的基类里已经把 selectionStyle 设置为None,可是想要多选必须将其打开,你们切记。orm

三、不喜欢系统的选中状态,可更改选中状态的背景(下面也是在自定义cell内部)cdn

UIView *view = [[UIView alloc] init];
 view.backgroundColor = UIColorFromRGB(0xF6F6F6);
 self.selectedBackgroundView = view;复制代码

四、右上角点击事件blog

[self.viewModel.rightViewModel.clickSubject subscribeNext:^(id x) {

        @strongify(self);

        if (self.mainTableView.editing) {

            self.viewModel.rightViewModel.title = @"编辑";

            [UIView animateWithDuration:0.5 animations:^{

                [self.mainTableView mas_remakeConstraints:^(MASConstraintMaker *make) {

                    @strongify(self);
                    make.edges.equalTo(self);
                }];

            }];

        } else {

            self.viewModel.rightViewModel.title = @"肯定";

            [UIView animateWithDuration:0.5 animations:^{

                [self.mainTableView mas_remakeConstraints:^(MASConstraintMaker *make) {

                    @strongify(self);
                    make.left.right.top.equalTo(self);
                    make.bottom.equalTo(-50);
                }];

            }];
        }
        [self.mainTableView setEditing:!self.mainTableView.editing animated:YES];
    }];复制代码

五、右下角删除事件

[[[self.deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {

        @strongify(self);
        NSMutableArray *deleteArray = [NSMutableArray array];
        for (NSIndexPath *indexPath in self.mainTableView.indexPathsForSelectedRows) {
            [deleteArray addObject:self.viewModel.dataArray[indexPath.row]];
        }

        NSMutableArray *currentArray = self.viewModel.dataArray;
        [currentArray removeObjectsInArray:deleteArray];
        self.viewModel.dataArray = currentArray;

        [self.mainTableView deleteRowsAtIndexPaths:self.mainTableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];//删除对应数据的cell

        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{

            @strongify(self);
            [self.mainTableView reloadData];
        });

    }];复制代码

4、单个删除(分为系统左滑,和点击cell上删除按钮两种)

一、系统左滑

#pragma mark - delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

    return @"删除此经验";
}

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

    [self.viewModel.deleteCommand execute:indexPath];
}复制代码

说明:删除操做数据及UI刷新和多选是一致的,就不上代码了,这里只需注意左滑须要遵循的系统代理就行。

二、点击Cell删除

与系统左滑删除的不一样仅仅是手动触发删除事件而已。

[[[self.deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) {

        [viewModel.deleteCommand execute:nil];
    }];复制代码

单个删除的操做数据和UI刷新也上下代码吧(虽然有些重复o(╯□╰)o)

[[self.viewModel.deleteSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSIndexPath *indexPath) {

        @strongify(self);

        if (self.viewModel.dataArray.count > indexPath.row) {

            [self.viewModel.dataArray removeObjectAtIndex:indexPath.row];  //删除数组里的数据
            [self.mainTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//删除对应数据的cell

            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{

                 @strongify(self);
                 [self.mainTableView reloadData];
            });
        }

    }];复制代码

5、tableView的一些Tips(不经常使用的,或没注意的)

一、设置tableView可不能够选中(防止cell重复点击也能够利用这条特性)

self.tableView.allowsSelection = NO;复制代码

二、容许tableview多选

self.tableView.allowsMultipleSelection = YES;复制代码

三、编辑模式下是否能够选中

self.tableView.allowsSelectionDuringEditing = NO;复制代码

四、编辑模式下是否能够多选

self.tableView.allowsMultipleSelectionDuringEditing = YES;复制代码

五、获取被选中的全部行

[self.tableView indexPathsForSelectedRows]复制代码

六、获取当前可见的行

[self.tableView indexPathsForVisibleRows];复制代码

七、 改变UITableViewCell选中时背景色

cell.selectedBackgroundView.backgroundColor复制代码

八、自定义UITableViewCell选中时背景

cell.selectedBackgroundView复制代码

九、自定义UITableViewCell选中时系统label字体颜色

cell.textLabel.highlightedTextColor复制代码

十、设置tableViewCell间的分割线的颜色

[theTableView setSeparatorColor:[UIColor xxxx ]];复制代码

十一、pop返回table时,cell自动取消选中状态(在viewWillAppear中添加以下代码)

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];复制代码

十二、点击后,过段时间cell自动取消选中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}
- (void)deselect {
    [self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES];
}复制代码

本文由做者 王隆帅 编写,转载请保留版权网址,感谢您的理解与分享,让生活变的更美好!