記錄一下項目中遇到的tableviewCell多行選擇刪除和全選刪除募胃。
1 首先 創(chuàng)建數(shù)組
2 創(chuàng)建tableview
self.tableView.editing = NO;//默認(rèn)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];
_deleteBtn.enabled = NO;
[_baseView addSubview:_deleteBtn];
4 tableview的delegate和datasource
//是否可以編輯 默認(rèn)的時YES
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//選擇編輯的方式,按照選擇的方式對表進(jìn)行處理
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {//刪除
//真正項目中做刪除
//1.將表中的cell刪除
//2.將本地的數(shù)組中數(shù)據(jù)刪除
//3.最后將服務(wù)器端的數(shù)據(jù)刪除
}
}
//選擇你要對表進(jìn)行處理的方式 默認(rèn)是刪除方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//選中時將選中行的在self.dataArray 中的數(shù)據(jù)添加到刪除數(shù)組self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
}
//取消選中時 將存放在self.deleteArr中的數(shù)據(jù)移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
}
實現(xiàn)完tableview的代理方法 下面處理各個按鈕的響應(yīng)事件
1 首先是選擇按鈕的響應(yīng)事件 在按鈕事件里面要有self.tableView.allowsMultipleSelectionDuringEditing = YES; 允許支持同時選中多行.
然后在點擊的時候讓tableview.editing = !tableview.editing 點擊此按鈕可切換tableview的編輯狀態(tài)
//選擇按鈕點擊響應(yīng)事件
- (void)selectedBtn:(UIButton *)button {
_deleteBtn.enabled = YES;
//支持同時選中多行
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 全選按鈕的響應(yīng)事件
點擊全選按鈕時 要在這里選中所有的cell 我在網(wǎng)上看到很多資料都是選中當(dāng)前可見的cell 我們項目要求是全部cell 参袱,所以在這里我這樣去做 在self.dataArray里面遍歷抹蚀, 然后找到對應(yīng)的一共多少行, 獲取索引值 indexPath环壤,tableview有系統(tǒng)方法 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 選擇全部的cell 此時在這個全選方法中將數(shù)據(jù)源self.dataArray的所有數(shù)據(jù)全部添加到self.deleteArr (存儲刪除數(shù)據(jù)的數(shù)組中)
//全選
- (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 刪除按鈕的處理事件
無論是多行刪除還是全部刪除的數(shù)據(jù)對tableview的操作都是走這個delete方法的。在方法中判斷當(dāng)前的tableview是否處于編輯狀態(tài)郑现。然后再執(zhí)行刪除操作荧降,關(guān)鍵點就是將數(shù)據(jù)源self.dataArray中的要刪除的數(shù)據(jù)移除,之前我們的多選或者全選已經(jīng)將我們要刪除的數(shù)據(jù)存儲在self.deleteArr中了 朵诫,所以在這里我們用[self.dataArray removeObjectsInArray:self.deleteArr];這個方法操作 然后刷新tableview。至此就可以實現(xiàn)功能了剪返。
- (void)deleteClick:(UIButton *) button {
if (self.tableView.editing) {
//刪除
[self.dataArray removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
}
else return;
}
總結(jié) :這里面一共有以下幾個點
1 在多選刪除時 在didSelectRowAtIndexPath這個方法中,根據(jù)cell所在行的索引值將此行的數(shù)據(jù)存到self.deleteArr中[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
當(dāng)取消刪除時脱盲,要將self.deleteArr中的數(shù)據(jù)移除,不然會造成 (你先選中一行 然后取消選中 但是當(dāng)你點擊刪除按鈕時,這行cell還是會被刪除)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
}
2 在全選時殿遂。將self.dataArr 中的全部數(shù)據(jù)都賦值給self.deleteArr
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArray];
3 執(zhí)行刪除操作時,將self.dataArr中包含self.deleteArr的數(shù)據(jù)移除
self.dataArray removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
后記:還有一個在cell中添加長按手勢 使tableview進(jìn)入編輯狀態(tài)
在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
方法中 為cell添加長按手勢
UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressedAct:)];
longPressed.minimumPressDuration = 1;
[cell.contentView addGestureRecognizer:longPressed];
實現(xiàn)長按手勢的響應(yīng)方法:
-(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;
}
}