iOS 11 停止滑动 UITableViewCell 的默认行为

iOS 11 stop default behaviour of swipe UITableViewCell

在我的应用程序中,一个屏幕有 table 个视图并显示 "Edit""Delete" 按钮滑动 UITableViewCell

一切正常,但问题出在 iOS 11 中 UITableViewCell 的默认动画中。

我知道 iOS11 中的一些行为和基础发生了变化。当你继续拉动你的单元格时,editActionsForRowAt 方法将被自动调用

有时它很好,但是当你在 table 上添加一个按钮时,滑动然后看起来很丑陋。

请看下面,iOS 10 和 iOS 11 中滑动单元格的默认行为。

iOS 10:

iOS11:

您可以在iOS 11中看到如果我拉动滑动然后自动editActionsForRowAt方法os调用并显示警报消息。

我的问题:有什么原因可以阻止 iOS 11 的这种行为并为此编写代码?

因为如果您的 table 有超过 2 个按钮,那么它看起来很难看。

这似乎符合您的要求。然而,动画与 iOS 10 中的动画并不完全相同,但它消除了完全滑动,并且看起来仍然不错。

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let deleteAction = UIContextualAction.init(style: UIContextualAction.Style.destructive, title: "Delete", handler: { (action, view, completion) in
        //TODO: Delete
        completion(true)
    })

    let editAction = UIContextualAction.init(style: UIContextualAction.Style.normal, title: "Edit", handler: { (action, view, completion) in
        //TODO: Edit
        completion(true)
    })

    let config = UISwipeActionsConfiguration(actions: [deleteAction, editAction])

    config.performsFirstActionWithFullSwipe = false
    return config
}

关键行是config.performsFirstActionWithFullSwipe = false,因为这将禁用完全滑动。

以上代码将覆盖 iOS 11 上的 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath),但您的 editActionsforRowAt 仍将用于子 iOS 10 设备。