iOS 多選刪除功能附tableViewTips及單選刪除
一、前言
這次分享并記錄一下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];//刪除對(duì)應(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];//刪除對(duì)應(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及單選刪除,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
ios彈幕高效加載實(shí)現(xiàn)方式實(shí)例代碼
看到密密麻麻的彈幕第一印象就是怎么樣高效加載來避免卡頓,這篇文章主要介紹了ios彈幕高效加載實(shí)現(xiàn)方式實(shí)例代碼,有興趣的可以了解一下。2017-03-03
iOS 指壓即達(dá)集成iOS9里的3D Touch的方法
這篇文章主要介紹了iOS 指壓即達(dá)集成iOS9里的3D Touch的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法
這篇文章主要介紹了iOS App中調(diào)用相冊(cè)中圖片及獲取最近的一張圖片的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
OC runtime學(xué)習(xí)筆記之關(guān)聯(lián)對(duì)象
這篇文章主要介紹了OC runtime學(xué)習(xí)筆記之關(guān)聯(lián)對(duì)象的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Objective-C的MKNetworkKit開發(fā)框架解析
這篇文章主要介紹了Objective-C的MKNetworkKit開發(fā)框架解析,MKNetworkKit是一個(gè)用于iOS開發(fā)的輕量級(jí)框架,需要的朋友可以參考下2015-11-11
Swift 去除 TableView 多余的空Cell中的橫線的方法
這篇文章主要介紹了Swift 去除 TableView 多余的空Cell中的橫線的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

