如何启用滑动以删除TableView中的单元格? [英] How to enable swipe to delete cell in a TableView?

查看:82
本文介绍了如何启用滑动以删除TableView中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController,它实现了TableViews委托和数据源协议. 现在,我要向单元格添加滑动删除"手势.

I have a UIViewController that implements TableViews delegate and datasource protocols. Now I want to add "swipe to delete" gesture to cells.

我应该怎么做.

我给出了commitEditingStyle方法的空白实现,并且还将Editing属性设置为YES.

I have given a blank implementation of commitEditingStyle method and also set the Editing property to YES.

仍然没有滑动功能.

现在我需要分别在每个单元格中添加UISwipeGesture吗?

Now Do I need to separately add UISwipeGesture to each cell ?

或者我想念什么吗?

推荐答案

如果需要在单元格滑动时显示删除"按钮,则不必设置editing:YES.您必须实现tableView:canEditRowAtIndexPath:并从此处为需要编辑/删除的行返回YES.当tableView的dataSource是UITableViewContoller的子类时,则不需要此方法-如果未重写此方法,则默认情况下返回YES.在其他所有情况下,您都必须实现它.

You don't have to set editing:YES if you need to show Delete button on cell swipe. You have to implement tableView:canEditRowAtIndexPath: and return YES from there for rows you need to edit/delete. This is not necessary when your tableView's dataSource is a subclass of UITableViewContoller - this method, if not overridden, returns YES by default. In all other cases you have to implement it.

我们一起发现了问题-如果表未处于编辑模式,则tableView:editingStyleForRowAtIndexPath:返回UITableViewCellEditingStyleNone.

Together we have found the problem - tableView:editingStyleForRowAtIndexPath: returned UITableViewCellEditingStyleNone if table wasn't in editing mode.

这篇关于如何启用滑动以删除TableView中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆