我想知道如何在删除单元格之前显示删除确认(警报)。单元格是带有信息的字符,因此如果用户错误地删除(滑动)一个单元格将是很糟糕的。
这是允许我删除行的代码段。我正在使用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])
}
答案 0 :(得分:0)
您可以尝试
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)
let alertView = UIAlertController(title: "", message: "Are you sure you want to delete the item ? ", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
self.namesArray.remove(at: indexPath.row)
self.imagesArray.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
})
let cancelAction = UIAlertAction(title: "Cancel", style:.cancel, handler: { (alert) in
print("Cancel")
})
alertView.addAction(okAction)
alertView.addAction(cancelAction)
self.present(alertView, animated: true, completion: nil)
})
modifyAction.image = UIImage(named: "delete")
modifyAction.backgroundColor = .purple
return UISwipeActionsConfiguration(actions: [modifyAction])
}
答案 1 :(得分:0)
您可以拉出代码以在单独的方法中显示UIAlertController
,当用户按下Delete键时可以调用该方法。这是下面的代码,确保闭包中包含的所有表示逻辑都被调度到主线程。
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)
}