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

2、然后編輯圖如下:

3、編輯步驟:
點(diǎn)擊右上角按鈕編輯,界面呈現(xiàn)編輯狀態(tài)底部刪除按鈕彈出
選擇刪除cell項(xiàng),點(diǎn)擊右下角刪除可刪除
點(diǎn)擊右上角,退出編輯狀態(tài),底部刪除按鈕退出界面
三、多選刪除核心代碼
1、設(shè)置允許tableView編輯狀態(tài)下允許多選
_mainTableView.allowsMultipleSelectionDuringEditing = YES;
2、將cell設(shè)置為可選擇的風(fēng)格(下面代碼是在自定義Cell內(nèi)部)
self.selectionStyle = UITableViewCellSelectionStyleDefault;
說明:因?yàn)樽哉J(rèn)為系統(tǒng)的選中狀態(tài)很難看,所以在cell的基類里已經(jīng)把 selectionStyle 設(shè)置為None,但是想要多選必須將其打開,大家切記。
3、不喜歡系統(tǒng)的選中狀態(tài),可更改選中狀態(tài)的背景(下面也是在自定義cell內(nèi)部)
UIView *view = [[UIView alloc] init]; view.backgroundColor = UIColorFromRGB(0xF6F6F6); self.selectedBackgroundView = view;
4、右上角點(diǎn)擊事件
[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];//刪除對應(yīng)數(shù)據(jù)的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]; }); }];四、單個(gè)刪除(分為系統(tǒng)左滑,和點(diǎn)擊cell上刪除按鈕兩種)
1、系統(tǒng)左滑
#pragma mark - delete- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete;}- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"刪除此經(jīng)驗(yàn)";}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { [self.viewModel.deleteCommand execute:indexPath];}說明:刪除操作數(shù)據(jù)及UI刷新和多選是一致的,就不上代碼了,這里只需注意左滑需要遵循的系統(tǒng)代理就行。
2、點(diǎn)擊Cell刪除
與系統(tǒng)左滑刪除的不同僅僅是手動(dòng)觸發(fā)刪除事件而已。
[[[self.deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id x) { [viewModel.deleteCommand execute:nil]; }];單個(gè)刪除的操作數(shù)據(jù)和UI刷新也上下代碼吧!(雖然有些重復(fù)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]; //刪除數(shù)組里的數(shù)據(jù) [self.mainTableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//刪除對應(yīng)數(shù)據(jù)的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]; }); } }];五、tableView的一些Tips(不常用的,或沒注意的)
1、設(shè)置tableView可不可以選中(防止cell重復(fù)點(diǎn)擊也可以利用這條特性)
self.tableView.allowsSelection = NO;
2、允許tableview多選
self.tableView.allowsMultipleSelection = YES;
3、編輯模式下是否可以選中
self.tableView.allowsSelectionDuringEditing = NO;
4、編輯模式下是否可以多選
self.tableView.allowsMultipleSelectionDuringEditing = YES;
5、獲取被選中的所有行
[self.tableView indexPathsForSelectedRows]
6、獲取當(dāng)前可見的行
[self.tableView indexPathsForVisibleRows];
7、 改變UITableViewCell選中時(shí)背景色
cell.selectedBackgroundView.backgroundColor
8、自定義UITableViewCell選中時(shí)背景
cell.selectedBackgroundView
9、自定義UITableViewCell選中時(shí)系統(tǒng)label字體顏色
cell.textLabel.highlightedTextColor
10、設(shè)置tableViewCell間的分割線的顏色
[theTableView setSeparatorColor:[UIColor xxxx ]];
11、pop返回table時(shí),cell自動(dòng)取消選中狀態(tài)(在viewWillAppear中添加如下代碼)
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
12、點(diǎn)擊后,過段時(shí)間cell自動(dòng)取消選中
- (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];}以上所述是小編給大家介紹的AniOS 多選刪除功能附tableViewTips及單選刪除,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選