一、前言
這次分享并記錄一下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( 主站蜘蛛池模板: 岳普湖县| 彩票| 汽车| 麻阳| 郎溪县| 太康县| 梅河口市| 大城县| 得荣县| 衡山县| 敦煌市| 绥中县| 江山市| 台湾省| 石楼县| 淮北市| 淮安市| 漳浦县| 池州市| 澳门| 门源| 界首市| 河池市| 浦江县| 涪陵区| 和龙市| 岢岚县| 大关县| 临猗县| 鄄城县| 黄龙县| 辰溪县| 石河子市| 苏州市| 商丘市| 明星| 霞浦县| 佛教| 观塘区| 株洲市| 乡宁县|