UITableView、UISwipeActionsConfiguration、UIContextualAction - pullView 不在视图层次结构中。这是一个 UIKit 错误

UITableView, UISwipeActionsConfiguration, UIContextualAction - The pullView is not in a view hierarchy. This is a UIKit bug

我有一个带有滑动动作的 UITableView,当滑动时:A) 显示滑动动作按钮应该显示的 blank/empty 区域;和 B) 以下行记录到 Xcode 中的调试控制台:

[Assert] The pullView is not in a view hierarchy. This is a UIKit bug.

截至今天,搜索该问题标题的结果为零 Google 个。

原来罪魁祸首是下面的

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if !editing {
        rowSelectionState.removeAll()
        loadData()
    }
    refreshView()
}

具体来说,包含 tableView.reloadData() 调用的 refreshView() 调用需要位于 if !editing { ... } 块内。否则,当启动滑动操作时,滑动操作似乎调用 setEditMode(true, ...),从而调用 tableView.reloadData(),这会扰乱 UISwipeActionsConfiguration 的正确显示能力。

因此上面应该是这样的:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if !editing {
        rowSelectionState.removeAll()
        loadData()
        refreshView()
    }
}