gpt4 book ai didi

ios - 删除单元格之前提醒 Controller Swift Xcode

转载 作者:行者123 更新时间:2023-11-30 11:13:06 24 4
gpt4 key购买 nike

我想知道如何在删除单元格之前显示删除确认(警报)。这些单元格是带有信息的字符,因此如果用户错误地删除(滑动)单元格,那将是很糟糕的。

这是允许我删除行的代码片段。我使用 Xcode 10/Swift

// Delete Rows
override func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)

self.namesArray.remove(at: indexPath.row)
self.imagesArray.remove(at: indexPath.row)

self.tableView.deleteRows(at: [indexPath], with: .fade)

})
modifyAction.image = UIImage(named: "delete")
modifyAction.backgroundColor = .purple

return UISwipeActionsConfiguration(actions: [modifyAction])

}

最佳答案

您可以将显示 UIAlertController 的代码拉出到一个单独的方法中,您可以在用户按删除键时调用该方法。下面是代码,确保闭包中包含的所有表示逻辑都被分派(dispatch)到主线程。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let modifyAction = UIContextualAction(style: .normal, title: "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
DispatchQueue.main.async {
self.showDeleteWarning(for: indexPath)
}

success(true)
})

modifyAction.image = UIImage(named: "delete")
modifyAction.backgroundColor = .purple

return UISwipeActionsConfiguration(actions: [modifyAction])
}

func showDeleteWarning(for indexPath: IndexPath) {
//Create the alert controller and actions
let alert = UIAlertController(title: "Warning Title", message: "Warning Message", preferredStyle: .alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { _ in
DispatchQueue.main.async {
self.namesArray.remove(at: indexPath.row)
self.imagesArray.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
}

//Add the actions to the alert controller
alert.addAction(cancelAction)
alert.addAction(deleteAction)

//Present the alert controller
present(alert, animated: true, completion: nil)
}

关于ios - 删除单元格之前提醒 Controller Swift Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51993719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com