UITableView左右滑动删除常见问题
2015-09-19 12:17
453 查看
iOS中的UITableView,在上面滑动时,可以出现一个“删除”按钮,来进行删除操作。
大概以有下几个要素:
1.UITableViewDelegate中的下面一个方法:
- (UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath;
这个方法可以不实现,默认会是响应滑动手势,显示“删除”按钮的效果。如果实现的话,一定要返回类型:UITableViewCellEditingStyleDelete。否则在cell上左右滑动时,不会出现“删除”按钮。
2- (BOOL)tableViewUITableView *)tableView canMoveRowAtIndexPathNSIndexPath *)indexPath;
这个方法表示判断对应的indexPath的cell是否可以编辑。可以不实现,默认是可以编辑的,如果实现的话,一定要返回YES。否则出不来“删除”按钮。
3.-(void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath;
返回你想显示的文本就可以了。
这个方法一定要实现,因为系统会判断你是否实现了这个方法,只有实现了这个方法,在cell上左右滑动时才会出现“删除”按钮。
总之,正确实现上面三个方法,“删除”按钮应该会显示出来。
还要别忘了指定tableView的dataSource和delegate啊,不然一切都是白费的。
小技巧:如果感觉“删除”两个字太呆板,可以自定义这个按钮的显示文字的,需要实现UITableViewDelegate的如下方法:
- (NSString *)tableViewUITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPathNSIndexPath *)indexPath
大概以有下几个要素:
1.UITableViewDelegate中的下面一个方法:
- (UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath;
这个方法可以不实现,默认会是响应滑动手势,显示“删除”按钮的效果。如果实现的话,一定要返回类型:UITableViewCellEditingStyleDelete。否则在cell上左右滑动时,不会出现“删除”按钮。
2- (BOOL)tableViewUITableView *)tableView canMoveRowAtIndexPathNSIndexPath *)indexPath;
这个方法表示判断对应的indexPath的cell是否可以编辑。可以不实现,默认是可以编辑的,如果实现的话,一定要返回YES。否则出不来“删除”按钮。
3.-(void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath;
返回你想显示的文本就可以了。
这个方法一定要实现,因为系统会判断你是否实现了这个方法,只有实现了这个方法,在cell上左右滑动时才会出现“删除”按钮。
总之,正确实现上面三个方法,“删除”按钮应该会显示出来。
还要别忘了指定tableView的dataSource和delegate啊,不然一切都是白费的。
小技巧:如果感觉“删除”两个字太呆板,可以自定义这个按钮的显示文字的,需要实现UITableViewDelegate的如下方法:
- (NSString *)tableViewUITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPathNSIndexPath *)indexPath
相关文章推荐
- 真阳率(true positive rate)、假阳率(false positive rate),AUC,ROC
- iOS UICollectionView简单使用
- iOS6新特征:UICollectionView介绍 --UICollectionView不错的文章
- [裸KMP][HDU1711][Number Sequence]
- [裸KMP][HDU1711][Number Sequence]
- iOS经典讲解之妙用UITextView
- 方便的任意处使用UI线程的工具类分享
- Looper/Handler/MessageQueue/Message/AsyncResult/Registrant/RegistrantList
- UITableView中Cell重用机制导致内容重复解决方法
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
- 字符串的“压缩”
- USACO 2.1.3 Sorting a Three-Valued Sequence
- 基础知识之_int、NSInteger、NSUInteger、NSNumber的区别和联系
- PAT-PAT (Advanced Level) Practise 1101 Quick Sort (25)【二星级】
- 记录一次EasyUI 的treegrid优化实践
- 权限控制easyui datagrid filed里面的内容
- emacs quick open and jump file (or buffer) which name is current word
- hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】
- 使用uiautomation自动化重命名pdf书签,使全大写字母变成首字母大写
- iOS UIGestureRecognizer手势总结