滑动以删除和“更多"按钮(就像在 iOS 7 上的邮件应用程序中一样)

问题描述:

当用户在表格视图中滑动单元格时如何创建更多"按钮(如 ios 7 中的邮件应用程序)

How to create a "more" button when user swipe a cell in table view (like mail app in ios 7)

我一直在此处和 Cocoa Touch 论坛中寻找此信息,但似乎找不到答案,我希望比我更聪明的人能给我一个解决方案.

I have been looking for this information both here and in the Cocoa Touch forum, but I cannot seem to find the answer and I am hoping someone smarter than myself can give me a solution.

我希望当用户滑动表格视图单元格时,显示多个编辑按钮(他默认为删除按钮).在 iOS 7 的邮件应用中,您可以滑动删除,但会显示一个更多"按钮.

I would like that when the user swipes a table view cell, to display more than one editing button (he default is the delete button). In the Mail app for iOS 7 you can swipe to delete, but there is a "MORE" button that shows up.

如何实施

iOS 8 好像开放了这个 API.Beta 2 中提供了此类功能的提示.

How to Implement

It looks like iOS 8 opens up this API. Hints of such functionality are present in Beta 2.

要使某些事情起作用,请在 UITableView 的委托上实现以下两个方法以获得所需的效果(示例请参见要点).

To get something working, implement the following two methods on your UITableView's delegate to get the desired effect (see gist for an example).

- tableView:editActionsForRowAtIndexPath:
- tableView:commitEditingStyle:forRowAtIndexPath:


文档说 tableView:commitEditingStyle:forRowAtIndexPath 是:

The documentation says tableView:commitEditingStyle:forRowAtIndexPath is:

未使用 UITableViewRowAction 调用编辑操作 - 将调用操作的处理程序."

"Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead."

但是,没有它,滑动就无法工作.即使方法存根是空白的,它现在仍然需要它.这显然是 beta 2 中的一个错误.

However, the swiping doesn't work without it. Even if the method stub is blank, it still needs it, for now. This is most obviously a bug in beta 2.


https://twitter.com/marksands/status/481642991745265664https://gist.github.com/marksands/76558707f583dbb8f870

原答案:https://*.com/a/24540538/870028


此工作的示例代码(在 Swift 中):http://dropbox.com/s/0fvxosft2mq2v5m/DeleteRowExampleSwift.zip

Sample code with this working (In Swift): http://dropbox.com/s/0fvxosft2mq2v5m/DeleteRowExampleSwift.zip

示例代码在 MasterViewController.swift 中包含此易于遵循的方法,仅使用此方法您就可以获得 OP 屏幕截图中显示的行为:

The sample code contains this easy-to-follow method in MasterViewController.swift, and with just this method you get the behavior shown in the OP screenshot:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

    var moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
        println("MORE•ACTION");
    });
    moreRowAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0);

    var deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
        println("DELETE•ACTION");
    });

    return [deleteRowAction, moreRowAction];
}