記錄一下項目中遇到的tableviewCell多行選擇刪除和全選刪除。
效果圖如上圖
1 首先 創建數組
2 創建tableview?
self.tableView.editing = NO;//默認tableview的editing 是不開啟的
3 全選和多選 刪除按鈕
//選擇按鈕
UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeSystem];
selectedBtn.frame = CGRectMake(0, 0, 60, 30);
[selectedBtn setTitle:@"選擇" forState:UIControlStateNormal];
[selectedBtn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn];
self.navigationItem.rightBarButtonItem =selectItem;
//? ? ? ? 全選
_selectAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_selectAllBtn.frame = CGRectMake(0, 0, 60, 30);
[_selectAllBtn setTitle:@"全選" forState:UIControlStateNormal];
[_selectAllBtn addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllBtn];
self.navigationItem.leftBarButtonItem = leftItem;
_selectAllBtn.hidden = YES;
_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height- 60, self.view.frame.size.width, 60)];
_baseView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_baseView];
//刪除按鈕
_deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_deleteBtn.backgroundColor = [UIColor redColor];
[_deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];
_deleteBtn.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);
[_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
[_baseView addSubview:_deleteBtn];
4 tableview的delegate和datasource
//是否可以編輯? 默認的時YES
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//選擇編輯的方式,按照選擇的方式對表進行處理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {//刪除
//真正項目中做刪除
//1.將表中的cell刪除
//2.將本地的數組中數據刪除
//3.最后將服務器端的數據刪除
}
}
//選擇你要對表進行處理的方式? 默認是刪除方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
******//選中時將選中行的在self.dataArray 中的數據添加到刪除數組self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
}
******//取消選中時 將存放在self.deleteArr中的數據移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath? {
[self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}
實現完tableview的代理方法 下面處理各個按鈕的響應事件
1 首先是選擇按鈕的響應事件 在按鈕事件里面要有self.tableView.allowsMultipleSelectionDuringEditing = YES; 允許支持同時選中多行
然后在點擊的時候讓tableview.editing = !tableview.editing 點擊此按鈕可切換tableview的編輯狀態
- (void)selectedBtn:(UIButton *)button {
//支持同時選中多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = !self.tableView.editing;
if (self.tableView.editing) {
_selectAllBtn.hidden = NO;
[button setTitle:@"完成" forState:UIControlStateNormal];
}else{
_selectAllBtn.hidden = YES;
[button setTitle:@"刪除" forState:UIControlStateNormal];
}
}
2 全選按鈕的響應事件
點擊全選按鈕時 要在這里選中所有的cell 我在網上看到很多資料都是選中當前可見的cell 我們項目要求是全部cell ,所以在這里我這樣去做 在self.dataArray里面遍歷, 然后找到對應的一共多少行, 獲取索引值 indexPath,tableview有系統方法 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 選擇全部的cell ?此時在這個全選方法中將數據源self.dataArray的所有數據全部添加到self.deleteArr (存儲刪除數據的數組中)
//全選
- (void)selectAllBtnClick:(UIButton *)button {
//? ? [self.tableView reloadData];
for (int i = 0; i < self.dataArray.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArray];
}
NSLog(@"self.deleteArr:%@", self.deleteArr);
}
3 刪除按鈕的處理事件
無論是多行刪除還是全部刪除的數據對tableview的操作都是走這個delete方法的。在方法中判斷當前的tableview是否處于編輯狀態。然后再執行刪除操作,關鍵點就是將數據源self.dataArray中的要刪除的數據移除,之前我們的多選或者全選已經將我們要刪除的數據存儲在self.deleteArr中了 ,所以在這里我們用[self.dataArray removeObjectsInArray:self.deleteArr];這個方法操作 然后刷新tableview。至此就可以實現功能了。
- (void)deleteClick:(UIButton *) button {
if (self.tableView.editing) {
//刪除
[self.dataArray removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
}
else return;
}
總結 :這里面一共有以下幾個點?
1 在多選刪除時 在didSelectRowAtIndexPath這個方法中,根據cell所在行的索引值將此行的數據存到self.deleteArr中 [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
當取消刪除時,要將self.deleteArr中的數據移除,不然會造成 (你先選中一行 然后取消選中 但是當你點擊刪除按鈕時,這行cell還是會被刪除)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath? {
}
2 在全選時。將self.dataArr 中的全部數據都賦值給self.deleteArr
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArray];
3 執行刪除操作時,將self.dataArr中包含self.deleteArr的數據移除
self.dataArray removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
后記:還有一個在cell中添加長按手勢 使tableview進入編輯狀態
在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中 為cell添加長按手勢
UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressedAct:)];
longPressed.minimumPressDuration = 1;
[cell.contentView addGestureRecognizer:longPressed];
實現長按手勢的響應方法:
-(void)longPressedAct:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
//add your code here
self.tableView.editing = YES;
}
}