iOS开发之tableView实现左滑删除功能
2017-01-13 15:46
681 查看
前言
这几天要实现左划删除的功能,发现网上很多帖子大多出自一人之手,然后都是 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开发者们能有一定的帮助,如果有疑问大家可以留言交流。
您可能感兴趣的文章:
相关文章推荐
- iOS 开发-实现TableView的编辑功能以删除数据
- iOS开发:UITableView实现侧滑删除cell的功能
- iOS项目开发小技巧 (三) --UITableView实现Cell左划删除等自定义功能
- iOS中tableview实现编辑、全选及删除等功能的方法示例
- iOS开发中实现UITableView的Cell左划删除等自定义功能
- iOS 使用cell的删除功能,实现tableview的代理
- iOS 嵌套在scrollView里面的tableView实现左滑删除功能
- iOS开发之tableView cell的展开收回功能实现代码
- IOS开发系列之阿堂教程:tableView的下拉涮新功能实践
- iphone开发之实现UITableView多选删除功能详解
- iOS开发UI篇—在UItableview中实现加载更多功能
- iOS tableview 实现简单的搜索功能
- iOS开发UI篇—在UItableview中实现加载更多功能
- ((ios开发学习笔记九)) Simple TableView 实现(附 实例源码)
- iOS开发UI篇—在UItableview中实现加载更多功能
- IOS开发实现 UIScrollView 的循环轮播功能
- iOS tableview 实现简单的搜索功能
- IOS开发简单登录LoginViewController、注册RegisterViewController、UcenterViewController功能实现方法
- IOS开发笔记(2)EGOTableViewPullRefresh实现列表的下拉刷新功能
- iphone开发之实现UITableView多选删除功能详解