前言
这几天要实现左划删除的功能,发现网上很多帖子大多出自一人之手,然后都是 copy 的文章,其实都没有那么复杂,只实现一个代理方法就可以了
方法如下
?
1
2
3
4
5
6
7
8
9
10
11
|
- ( void )tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 删除数据源的数据,self.cellData是你自己的数据
[self.cellData removeObjectAtIndex:indexPath.row];
// 删除列表中数据
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
|
默认删除的文字为 Delete,要改为中文实现
?
1
2
3
4
|
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @ "删除" ; //默认文字为 Delete
}
|
下面这两个代理方法不用写也可以,默认就是这样
?
1
2
3
4
5
6
7
8
|
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
|
如果你报了这个错误:
?
1
|
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)
|
你把代理方法中这两个方法顺序搞混了,先删除数据,再删除 cell
[self.cellData removeObjectAtIndex:indexPath.row];
这个方法在前
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
这个方法在后
还有就是,别2到没设置代理,tableView.delegate = self;
总结
以上就是关于iOS利用tableView实现左划删除功能的全部内容了,希望本文的内容对给iOS开发者们能有一定的帮助,如果有疑问大家可以留言交流。
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 47
-
2025-05-27 48
-
2025-05-25 91
-
2025-05-29 85
-
浅谈C++类型转化(运算符重载函数)和基本运算符重载(自增自减)
2025-05-27 67
热门评论