此次分享並記錄一下tableView的多選刪除,並額外記錄一下cell的設置小技巧。數組
一、先上原圖ide
二、而後編輯圖以下:字體
三、編輯步驟:ui
**- 點擊右上角按鈕編輯,界面呈現編輯狀態底部刪除按鈕彈出spa
一、設置容許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];
});
}];複製代碼
一、系統左滑
#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];
});
}
}];複製代碼
一、設置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];
}複製代碼
本文由做者 王隆帥 編寫,轉載請保留版權網址,感謝您的理解與分享,讓生活變的更美好!