具有破坏性样式的UIContextualAction似乎默认删除行

Mur*_*gal 10 uitableview swipe ios11

我看到的问题是,当我创建一个UIContextualActionwith .destructive 传入truecompletionHandler,似乎有一个删除行的默认操作.

如果您从Xcode的模板创建一个新的Master-Detail应用程序并将此代码添加到MasterViewController...

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let testAction = UIContextualAction(style: .destructive, title: "Test") { (_, _, completionHandler) in
        print("test")
        completionHandler(true)
    }
    return UISwipeActionsConfiguration(actions: [testAction])
}
Run Code Online (Sandbox Code Playgroud)

您要滑动的行将被删除.请注意,没有代码更新表视图.此外,模型不会更新,如果向上滚动导致重新加载行,它将重新出现.

false在这种情况下传递不会删除该行.或使用.normal样式,true也不删除行.

.destructivetrue导致默认情况下删除行.

谁能解释这种行为?为什么要删除行?

Kri*_*lci 10

根据破坏性选项的文档:

删除数据或执行某种类型的破坏性任务的操作.

完成意味着表明行动是否成功.传递真实意味着破坏性任务是成功的,因此应该删除行.

您需要在发生破坏性操作时手动更新dataSource,否则会导致滚动以重新显示数据.您还需要告诉tableView数据已被删除.

下面是一些显示工作示例的代码:

UIContextualAction(style: .destructive, title: "Delete") { [weak self] (_, _, completion) in
    if self?.canDelete(indexPath) { // We can actually delete
        // remove the object from the data source
        self?.myData.remove(at: indexPath.row)
        // delete the row.  Without deleting the row or reloading the
        // tableview, the index will be off in future swipes
        self?.tableView?.deleteRows(at: [indexPath], with: .none)
        // Let the action know it was a success.  In this case the 
        // tableview will animate the cell removal with the swipe
        completion(true)    
    } else { // We can't delete for some reason
        // This resets the swipe state and nothing is removed from
        // the screen visually.
        completion(false)
    }         
}
Run Code Online (Sandbox Code Playgroud)

然后我需要重新加载tableview或调用deleteRows,以便indexPath在下一次滑动时正确计算.

如果我有10行并且我将第5行拖动以删除,那么除非tableview重新加载或者tableview被告知要以某种方式移除行,否则之后的每一行都将关闭一行.


mat*_*att 5

我无法在 iOS 13 中重现此问题。 iOS 12 及之前的行为是一个错误,或者它已被撤回(可能是因为它令人困惑)。

在 iOS 13 中,如果您只是.destructive通过调用从操作中返回completion(true)而不执行任何操作,则不会发生任何事情,这就是问题的结束。不会为您删除单元格。