一、前言
这次分享并记录一下tableview的多选删除,并额外记录一下单选删除及tableview的设置小技巧。
二、想要实现的效果图如下:
1、先上原图
2、然后编辑图如下:
3、编辑步骤:
点击右上角按钮编辑,界面呈现编辑状态底部删除按钮弹出
点击右上角,退出编辑状态,底部删除按钮退出界面
1、设置允许tableview编辑状态下允许多选
|
1
|
_maintableview.allowsmultipleselectionduringediting = yes;
|
2、将cell设置为可选择的风格(下面代码是在自定义cell内部)
|
1
|
self.selectionstyle = uitableviewcellselectionstyledefault;
|
说明:因为自认为系统的选中状态很难看,所以在cell的基类里已经把 selectionstyle 设置为none,但是想要多选必须将其打开,大家切记。
3、不喜欢系统的选中状态,可更改选中状态的背景(下面也是在自定义cell内部)
|
1
2
3
|
uiview *view = [[uiview alloc] init];
view.backgroundcolor = uicolorfromrgb(0xf6f6f6);
self.selectedbackgroundview = view;
|
4、右上角点击事件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[self.viewmodel.rightviewmodel.clicksubject subscribenext:^(id x) {
@strongify(self);
if (self.maintableview.editing) {
self.viewmodel.rightviewmodel.title = @"编辑";
[uiview animatewithduration:0.5 animations:^{
[self.maintableview mas_remakeconstraints:^(masconstraintmaker *make) {
@strongify(self);
make.edges.equalto(self);
}];
}];
} else {
self.viewmodel.rightviewmodel.title = @"确定";
[uiview animatewithduration:0.5 animations:^{
[self.maintableview mas_remakeconstraints:^(masconstraintmaker *make) {
@strongify(self);
make.left.right.top.equalto(self);
make.bottom.equalto(-50);
}];
}];
}
[self.maintableview setediting:!self.maintableview.editing animated:yes];
}];
|
5、右下角删除事件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[[[self.deletebtn rac_signalforcontrolevents:uicontroleventtouchupinside] takeuntil:self.rac_willdeallocsignal] subscribenext:^(id x) {
@strongify(self);
nsmutablearray *deletearray = [nsmutablearray array];
for (nsindexpath *indexpath in self.maintableview.indexpathsforselectedrows) {
[deletearray addobject:self.viewmodel.dataarray[indexpath.row]];
}
nsmutablearray *currentarray = self.viewmodel.dataarray;
[currentarray removeobjectsinarray:deletearray];
self.viewmodel.dataarray = currentarray;
[self.maintableview deleterowsatindexpaths:self.maintableview.indexpathsforselectedrows withrowanimation:uitableviewrowanimationleft];//删除对应数据的cell
dispatch_time_t delaytime = dispatch_time(dispatch_time_now, (int64_t)(1.0 * nsec_per_sec));
dispatch_after(delaytime, dispatch_get_main_queue(), ^{
@strongify(self);
[self.maintableview reloaddata];
});
}];
|
1、系统左滑
|
1
2
3
4
5
6
7
8
9
10
|
#pragma mark - delete
- (uitableviewcelleditingstyle)tableview:(uitableview *)tableview editingstyleforrowatindexpath:(nsindexpath *)indexpath {
return uitableviewcelleditingstyledelete;
}
- (nsstring *)tableview:(uitableview *)tableview titlefordeleteconfirmationbuttonforrowatindexpath:(nsindexpath *)indexpath {
return @"删除此经验";
}
- (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath {
[self.viewmodel.deletecommand execute:indexpath];
}
|
说明:删除操作数据及ui刷新和多选是一致的,就不上代码了,这里只需注意左滑需要遵循的系统代理就行。
2、点击cell删除
|
1
2
3
|
[[[self.deletebtn rac_signalforcontrolevents:uicontroleventtouchupinside] takeuntil:self.rac_prepareforreusesignal] subscribenext:^(id x) {
[viewmodel.deletecommand execute:nil];
}];
|
单个删除的操作数据和ui刷新也上下代码吧!(虽然有些重复o(╯□╰)o)
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[[self.viewmodel.deletesubject takeuntil:self.rac_willdeallocsignal] subscribenext:^(nsindexpath *indexpath) {
@strongify(self);
if (self.viewmodel.dataarray.count > indexpath.row) {
[self.viewmodel.dataarray removeobjectatindex:indexpath.row]; //删除数组里的数据
[self.maintableview deleterowsatindexpaths:[nsmutablearray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationleft];//删除对应数据的cell
dispatch_time_t delaytime = dispatch_time(dispatch_time_now, (int64_t)(1.0 * nsec_per_sec));
dispatch_after(delaytime, dispatch_get_main_queue(), ^{
@strongify(self);
[self.maintableview reloaddata];
});
}
}];
|
五、tableview的一些tips(不常用的,或没注意的)
1、设置tableview可不可以选中(防止cell重复点击也可以利用这条特性)
|
1
|
self.tableview.allowsselection = no;
|
2、允许tableview多选
|
1
|
self.tableview.allowsmultipleselection = yes;
|
3、编辑模式下是否可以选中
|
1
|
self.tableview.allowsselectionduringediting = no;
|
4、编辑模式下是否可以多选
|
1
|
self.tableview.allowsmultipleselectionduringediting = yes;
|
5、获取被选中的所有行
|
1
|
[self.tableview indexpathsforselectedrows]
|
6、获取当前可见的行
|
1
|
[self.tableview indexpathsforvisiblerows];
|
7、 改变uitableviewcell选中时背景色
|
1
|
cell.selectedbackgroundview.backgroundcolor
|
8、自定义uitableviewcell选中时背景
|
1
|
cell.selectedbackgroundview
|
9、自定义uitableviewcell选中时系统label字体颜色
|
1
|
cell.textlabel.highlightedtextcolor
|
10、设置tableviewcell间的分割线的颜色
|
1
|
[thetableview setseparatorcolor:[uicolor xxxx ]];
|
11、pop返回table时,cell自动取消选中状态(在viewwillappear中添加如下代码)
|
1
|
[self.tableview deselectrowatindexpath:[self.tableview indexpathforselectedrow] animated:yes];
|
12、点击后,过段时间cell自动取消选中
|
1
2
3
4
5
6
7
|
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {
//消除cell选择痕迹
[self performselector:@selector(deselect) withobject:nil afterdelay:0.5f];
}
- (void)deselect {
[self.tableview deselectrowatindexpath:[self.tableview indexpathforselectedrow] animated:yes];
}
|
以上所述是小编给大家介绍的anios 多选删除功能附tableviewtips及单选删除,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
相关文章
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
在ASP.NET 2.0中操作数据之三十一:使用DataList来一行显示多条记录
2025-05-29 77 -
springboot 高版本后继续使用log4j的完美解决方法
2025-05-27 40 -
2025-06-04 54
-
2025-06-04 88
-
2025-05-29 13



