美文网首页IOS收藏swift3.0iOS新手学习
iOS tableviewCell的多行选择删除和全选删除

iOS tableviewCell的多行选择删除和全选删除

作者: 劉光軍_MVP | 来源:发表于2015-11-16 23:27 被阅读24037次

记录一下项目中遇到的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;

}

}

Markdown模式链接

相关文章

网友评论

  • Beyond无状态:博主 刷新tableView 之前选中状态没了 怎么保持啊
    劉光軍_MVP:@多醋多辣 感觉你设置的状态不对 你看看 点击选中按钮时 model中状态改变了没
    Beyond无状态:@劉光軍_Shine 我用model存的状态 在willDispaly里面设置的cell选中与否 但是就不能取消选中了 咋办
    劉光軍_MVP:@多醋多辣 你要把选中状态存起来呀
  • IMKel:排版有点糟糕,谢谢分享
    劉光軍_MVP:@IMKel 额。哈哈哈 这个是刚开始的时候写的 那时候还不太会用markdown呢。谢谢意见
  • 楚简约:这个tableViewCell是删除了,但是关闭App从新打开时, tableViewCell又存在了,怎么同时删除对应plist里面数据已经对应沙盒里面照片(或视频)文件.
  • 马野:tableView.allowsMultipleSelectionDuringEditing = true
    tableView.setEditing(true, animated: true)
  • 简书白兔:谢谢你的分享!发现一个小问题哈,当没有点击“选择”的时候,点击任意cell,这时点击“选择”,出现编辑模式,不作任何勾选,直接点删除,会删掉在点击“选择”之前 点击过的那个cell。处理方法,在点击“选择”的触发时间里面 先给deleteArr 做一次清空。
    劉光軍_MVP:@CrazyBub 哈哈哈 当时写的时候比较简单 没有想那么严谨 谢谢 谢谢
  • d053345b0b60:非常感谢你的思路,太感谢了,但是有个地方是不是写错了
    “2 全选按钮的响应事件”的 for 循环里的:[self.deleteArr addObjectsFromArray:self.dataArray]; 这句话应该放在 for 循环之外才对吧?
    d053345b0b60:@劉光軍_Shine 客气,客气,谢谢你的分享
    劉光軍_MVP:@Smile_521 谢谢提醒:smile:
  • 懒猫的鱼:刚好到全选的问题,给了很好的思绪谢谢
  • 黑曼巴蜜蜜:请问多个section的话,怎么删除对应的模型数据呢?
    劉光軍_MVP:@hiuhiuhiu 不客气 哈哈
    黑曼巴蜜蜜:@劉光軍_ 搞定了,谢谢啊
    劉光軍_MVP:@hiuhiuhiu 这得看你的数据源是怎么设置的了啊 多个section就先找到对应的section模型下的cell模型操作吧
  • JohnQ:有人遇到过我说的那个问题吗
  • JohnQ:在自定义cell的时候,点击选择这个时候tableview不向右移动
  • Hey_我是韩先生:在编译状态下 选中 让后向上滑 cell之间的线消失了 有没有方法解决?
  • 越天高:有木有例子啊
    劉光軍_MVP:@luoyannanfei 有啊。文章最后有代码链接
  • 追风筝的荧火虫:不错的文章,写得很详细
  • Mg明明就是你:不错呦!我也想写这个东西~
  • 寥寥十一:请问这个选中的标志只能重写才能显示在右侧吗?
    劉光軍_MVP:@荞一夏 系统的方法嘛。在系统提供的方法中 直接调用就可以啊
  • 李大戮:求贴Demo
    劉光軍_MVP:@李大帅 您好,这篇博客写的时候我还不懂用MarkDown模式,所以我又新写了一份,您可以找一下我最新的文章,名字是一样的,只是内容看起来舒服了,代码链接已贴
  • qiongyong:学习了 :smile:
  • Shumin_Wu:支持
  • LostAbaddon:代码请用代码块。
    在设置中将编辑器选择为Markdown编辑器,然后代码块的语法为:
    ```code-lang
    blablabla
    ```
    劉光軍_MVP:@塔塔酱 好的 不过网页版的设置功能挂了 我下次注意

本文标题:iOS tableviewCell的多行选择删除和全选删除

本文链接:https://www.haomeiwen.com/subject/usgkhttx.html