我是靠谱客的博主 强健犀牛,这篇文章主要介绍iOS中tableview实现编辑、全选及删除等功能的方法示例,现在分享给大家,希望可以做个参考。

前言

我们在日常开发过程中或多或少都会遇到tableview的各种功能,这里简单记录一下tableview的删除和全选删除功能,废话不多说先看一下效果图


既然拿到了需求,就应该想一下如何去实现了,对照上面图片的内容,应该如何实现呢?

看完上图之后发现用到的几个功能:

第一个:左滑删除

第二个:全选删除

左边滑动删除

实现几个代理方法后就可以了

复制代码
1
2
3
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; }

改变左滑后按钮的文字

复制代码
1
2
3
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete; }

滑动删除样式,有多中可选,这里返回删除样式

复制代码
1
2
3
4
5
6
7
8
9
- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.dataArray removeObjectAtIndex: indexPath.row]; [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView reloadData]; } }

删除点击方法,处理想要删除的数据

这里有一个需要注意点,一定要先更新数据源,在更新UI

左滑删除就这些代码了,是不是很easy,在来看全选的代码

全选删除

这里我用的是全选功能是系统的方法,没有自定义按钮

点击编辑按钮的时候设置tableview

复制代码
1
[_tableView setEditing:YES animated:YES];

返回全选的样式

复制代码
1
2
3
4
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert; }

这样就会出现左侧的选中框

再来就是全选按钮的实现方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i< self.dataArray.count; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop]; } if (self.deleteArray.count >0) { [self.deleteArray removeAllObjects]; } [self.deleteArray addObjectsFromArray:self.dataArray]; [btn setTitle:@"取消" forState:UIControlStateNormal];

当然取消全选也有方法

复制代码
1
2
3
4
5
for (int i = 0; i< self.dataArray.count; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; [_tableView deselectRowAtIndexPath:indexPath animated:NO]; }

通过全选按钮实现的选中方法,需要在方法里把所有数据都添加到想要删除的数组里面

通过点击tableviewcell选择删除对象的时候需要把想要删除的数据添加到删除数组里面

复制代码
1
2
3
4
5
6
7
8
9
10
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (self.btn.selected) { NSLog(@"选中"); [self.deleteArray addObject:[self.dataArray objectAtIndex:indexPath.row]]; }else{ NSLog(@"跳转下一页"); } }

再次点击取消选中的数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { if (self.btn.selected) { NSLog(@"撤销"); [self.deleteArray removeObject:[self.dataArray objectAtIndex:indexPath.row]]; }else{ NSLog(@"取消跳转"); } }

问题一:

按照以上方法实现之后就可以实现想要的功能,但是还有UI的问题,那就是选择之后会出现下图的问题


会有一层背景色的覆盖,想要了解的请看看这篇文章 https://www.uoften.com/article/117619.htm

问题二:

还有一个问题 ,在自定义的cell上面添加控件的时候一定要添加到self.contentView上面,否则会出现控件不随cell移动的问题

复制代码
1
[self.contentView addSubview:self.label];

结束

到这里这篇文章的内容基本算完结了,如果还是有不明白的我在此留下Demo链接,里面有更详细的注释,Demo没有做UI适配,想看效果的画在模拟器6,7上面运行最好

Demo地址:http://git.oschina.net/T1_mine/tableviewedit

本地下载地址:http://xiazai.uoften.com/201707/yuanma/tableviewedit(uoften.com).rar

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对靠谱客的支持。

最后

以上就是强健犀牛最近收集整理的关于iOS中tableview实现编辑、全选及删除等功能的方法示例的全部内容,更多相关iOS中tableview实现编辑、全选及删除等功能内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部