iOS开发中tableView左滑删除的详细介绍
前言
这几天要实现左划删除的功能,发现网上很多帖子大多出自一人之手,然后都是 copy 的文章,其实都没有那么复杂,只实现一个代理方法就可以了
方法如下
- (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,要改为中文实现
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";//默认文字为 Delete
}
下面这两个代理方法不用写也可以,默认就是这样
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
如果你报了这个错误:
'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开发中Quartz2D的使用方法
你了解过Quartz2D吗,它是iOS开发中比较常用的语言,今天技术小编就带你们来看一看iOS开发中Quartz2D的使用方法,相信你们了解完这篇文章后,会对此产生比较浓厚的兴趣。
-
iOS开发中static变量有什么作用
爱站技术频道小编现在来给你们分享下iOS开发中static变量有什么作用的文章,这里提供了三种作用方法给你们参考,有需要这方面知识点的小伙伴随时都可以来参考阅读。
-
iOS开发之圆形排列随机生成的思路
这篇文章主要给大家介绍了iOS开发之圆形排列随机生成的思路,小编在这里提供了详细的实例代码给你们查看,如果有需要,那就来看一看吧,一定能让你有不小的收获。
-
IOS开发之Socket的详细介绍
这篇文章主要介绍了IOS开发之Socket的详细内容,并给出了一些基本的实现方法,下面我们就来看看这篇文章的具体内容,相信你们了解完后对此有了不一样的认知。
-
iOS之识别手势gesture的介绍
这篇文章介绍是的iOS之识别手势gesture内容,需要了解这方面详细内容的朋友一定要来看一看,小编为你详细整理了这方面的知识点,接下来就一起来了解了解吧!
