作者:手浪用户穹谷之鹰 | 来源:互联网 | 2023-01-10 12:04
我看到的问题是,当我创建一个UIContextualAction
with .destructive
并传入true
时completionHandler
,似乎有一个删除行的默认操作.
如果您从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])
}
您要滑动的行将被删除.请注意,没有代码更新表视图.此外,模型不会更新,如果向上滚动导致重新加载行,它将重新出现.
false
在这种情况下传递不会删除该行.或使用.normal
样式,true
也不删除行.
.destructive
并true
导致默认情况下删除行.
谁能解释这种行为?为什么要删除行?
1> Kris Gellci..:
根据破坏性选项的文档:
删除数据或执行某种类型的破坏性任务的操作.
完成意味着表明行动是否成功.传递真实意味着破坏性任务是成功的,因此应该删除行.
您需要在发生破坏性操作时手动更新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)
}
}
然后我需要重新加载tableview
或调用deleteRows
,以便indexPath
在下一次滑动时正确计算.
如果我有10行并且我将第5行拖动以删除,那么除非tableview
重新加载或者tableview
被告知要以某种方式移除行,否则之后的每一行都将关闭一行.