iOS中tableview实现编辑、全选及删除等功能的方法示例

时间:2021-05-02

前言

我们在日常开发过程中或多或少都会遇到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的问题,那就是选择之后会出现下图的问题

会有一层背景色的覆盖,想要了解的请看看这篇文章http://www.zzvips.com/article/149453.html

问题二:

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

? 1 [self.contentview addsubview:self.label];

结束

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

demo地址:http://git.oschina.net/t1_mine/tableviewedit

总结

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

原文链接:http://www.jianshu.com/p/d5f2e0d9b37a

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章