一、前言
這次分享并記錄一下tableView的多選刪除,并額外記錄一下單選刪除及tableView的設置小技巧。
二、想要實現的效果圖如下:
1、先上原圖

2、然后編輯圖如下:

3、編輯步驟:
點擊右上角按鈕編輯,界面呈現編輯狀態底部刪除按鈕彈出
選擇刪除cell項,點擊右下角刪除可刪除
點擊右上角,退出編輯狀態,底部刪除按鈕退出界面
三、多選刪除核心代碼
1、設置允許tableView編輯狀態下允許多選
_mainTableView.allowsMultipleSelectionDuringEditing = YES;
2、將cell設置為可選擇的風格(下面代碼是在自定義Cell內部)
self.selectionStyle = UITableViewCellSelectionStyleDefault;
說明:因為自認為系統的選中狀態很難看,所以在cell的基類里已經把 selectionStyle 設置為None,但是想要多選必須將其打開,大家切記。
3、不喜歡系統的選中狀態,可更改選中狀態的背景(下面也是在自定義cell內部)
UIView *view = [[UIView alloc] init]; view.backgroundColor = UIColorFromRGB(0xF6F6F6); self.selectedBackgroundView = view;
4、右上角點擊事件
[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]; }];5、右下角刪除事件
[[[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]; }); }];四、單個刪除(分為系統左滑,和點擊cell上刪除按鈕兩種)
1、系統左滑
#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刷新和多選是一致的,就不上代碼了,這里只需注意左滑需要遵循的系統代理就行。
2、點擊Cell刪除
與系統左滑刪除的不同僅僅是手動觸發刪除事件而已。
[[[self.deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) { [viewModel.deleteCommand execute:nil]; }];單個刪除的操作數據和UI刷新也上下代碼吧!(雖然有些重復o( 主站蜘蛛池模板: 凤山县| 瑞丽市| 昭觉县| 淮北市| 云南省| 阿荣旗| 剑川县| 城口县| 冕宁县| 柘荣县| 容城县| 彰化县| 东方市| 鄂温| 鄂伦春自治旗| 竹北市| 横峰县| 木里| 隆尧县| 西宁市| 贡山| 分宜县| 育儿| 樟树市| 尼木县| 福建省| 长岭县| 太康县| 神农架林区| 阜阳市| 五莲县| 全南县| 辽宁省| 敦煌市| 龙海市| 伊宁县| 嘉禾县| 博客| 广河县| 娄底市| 雅安市|