怎么在iOS中利用tableView实现一个左滑删除功能

发布时间:2021-01-19 16:42:20 作者:Leah
来源:亿速云 阅读:311

怎么在iOS中利用tableView实现一个左滑删除功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

方法如下

- (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实现一个左滑删除功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. iOS中tableView的分类有哪些
  2. 怎么在微信小程序中实现一个item左滑删除功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios tableview

上一篇:怎么在JAVA中利用递归删除文件

下一篇:利用易语言怎么删除指定的文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》