首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

UITalbeView系列-滑动展示“删除”

2013-11-29 
UITalbeView系列-滑动显示“删除”腾讯的QQ和微信中都有这样的操作,对一个UITableView中的cell,向左滑动的时

UITalbeView系列-滑动显示“删除”

腾讯的QQ和微信中都有这样的操作,对一个UITableView中的cell,向左滑动的时候,会在对应的cell右侧(iOS7.0样式发生了一点变化)出现一个“删除”按钮

要达到这样的效果,完全用系统的就可以,不需要自定义一个Button,加到cell后面,同时给cell加手势捕获这个操作。

注意:如果在cell加一个手势(测试中我用的Pan),那么cell系统自己的就不生效了。

生效主要依赖3个代理方法(要是说成一个也行)
(1)- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
这是最主要的方法,如果要显示“删除”,把这个“按钮”的响应事件写好。

(2)- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

“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.”

就像API中说的一样,“如果不实现,所有的cell(默认都是可编辑的)都会有UITableViewCellEditingStyleDelete这个类型”,所以不写这个方法,照样能出现。但是,如果你对indexPath的cell返回了其他Style,那就没有了。
所以,根据具体逻辑和业务,灵活返回具体的Style

(3)- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath


“Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.”
在(2)中已经提到了,所有的cell都是默认可编辑的


总结:如果要单纯的实现“滑动出现删除按钮”,只需要实现(1)就OK了。
通过(2)(3)的配合,完成相应的逻辑和业务。
至于调用顺序:
(3)是在UITableViewDataSource中的,table显示的时候就调用了,处在第一响应位。
(2)如果某一行可以编辑,会调用这个返回具体的Style。
(1)则是点击了“按钮”以后才会进的。

附:
如果要更改按钮的title,很简单实现另外一个代理方法。
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

热点排行