UITableViewCell水平滑动时,有时候不出来delete按钮的原因

同样是使用系统自定义的样式,但是有时候delete按钮就不能滑动出现,原来是下面第三个原因,没有实现

tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

。本来以为按钮的展现只应该是展现层面的逻辑,没有实现这个回调,最多只应该是删除后没有反应才对。看来有点想当然了。

博客链接:http://www.cnblogs.com/yingkong1987/p/3183534.html


但是,有些时候,由于实现方法不对,滑动时,这个按钮却显示不出来。分析了一下原因,大概以有下几个要素:
1.UITableViewDelegate中的下面一个方法:
// Allows customization of the editingStyle for a particular cell located at ‘indexPath’. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法可以不实现,默认会是响应滑动手势,显示“删除”按钮的效果。如果实现的话,一定要返回类型:UITableViewCellEditingStyleDelete。否则在cell上左右滑动时,不会出现“删除”按钮。

2.UITableViewDataSource中的下面两个方法:
// Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法表示判断对应的indexPath的cell是否可以编辑。可以不实现,默认是可以编辑的,如果实现的话,一定要返回YES。否则出不来“删除”按钮。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法一定要实现,因为系统会判断你是否实现了这个方法,只有实现了这个方法,在cell上左右滑动时才会出现“删除”按钮。

总之,正确实现上面三个方法,“删除”按钮应该会显示出来。
还要别忘了指定tableView的dataSource和delegate啊,不然一切都是白费的。

小技巧:如果感觉“删除”两个字太呆板,可以自定义这个按钮的显示文字的,需要实现UITableViewDelegate的如下方法:
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
返回你想显示的文本就可以了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值