Ios 删除单元格Swift Xcode前通知控制器
我想知道如何在删除单元格之前显示删除确认(警报)。这些单元格是带有信息的字符,因此如果用户错误地删除(滑动)其中一个单元格,这将是不好的 这是允许我删除行的代码段。Im使用Xcode 10/SwiftIos 删除单元格Swift Xcode前通知控制器,ios,swift,tableview,alert,Ios,Swift,Tableview,Alert,我想知道如何在删除单元格之前显示删除确认(警报)。这些单元格是带有信息的字符,因此如果用户错误地删除(滑动)其中一个单元格,这将是不好的 这是允许我删除行的代码段。Im使用Xcode 10/Swift // Delete Rows override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) ->
// 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])
}
你可以试试
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])
}
您可以将显示
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)
}
只需在操作块中添加一个
UIAlertController
。不需要DispatchQueue.main.asynchy@Sh_Khan,在考虑了更多并进行了一些搜索之后,我看到人们提到,因为这些是用户发起的操作,他们相信它们将始终在主线程上被调用。你有任何书面证据吗?我明白你的意思,我也明白这是有道理的,如果苹果公司有一些明确的文档来保证这一点,那就更好了。@KyleH UIKit API在响应UI事件时总是在主队列上调用其代码。这是一个简单的事实。完美。谢谢您的时间。@AbelC没问题@rmaddy之前,苹果的委托方法中存在与用户发起的操作相关的表示逻辑问题。虽然这个雷达很旧(),但我今天仍然可以产生相同的延迟,其他人也可以产生最近的so posts/Github问题,等等。因此,您可以在我的代码中删除对主线程的调用,我只想确保您不会遇到与didselectrowatinex
方法类似的延迟。