这是我尝试创建的代码,但它不起作用,它首先显示打印行,然后创建“警报”,如果我选择是,它会在我下次单击“删除”时删除该行.
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
crearAlertaDoble(titulo: "¿Seguro que deseas eliminar este calendario?", mensaje: "")
print("opcion elegida: \(opcionAlertaMensaje)")
if (opcionAlertaMensaje == 1) {
objetoContenedor.calendarios.remove(at: indexPath.row) //WIP, MOSTRAR MENSAJE SI ESTA SEGURO
tableView.deleteRows(at: [indexPath], with: .fade)
opcionAlertaMensaje = 2
} else {
}
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
这是警报代码:
func crearAlertaDoble(titulo: String, mensaje: String) {
let alert = UIAlertController(title: titulo, message: mensaje, preferredStyle: .alert)
let botonUno = UIAlertAction(title: "NO!", style: UIAlertActionStyle.destructive, handler: { (action) -> Void in
self.opcionAlertaMensaje = 0
} )
let botonDos = UIAlertAction(title: "Si", style: UIAlertActionStyle.default, handler: { (action) -> Void in
self.opcionAlertaMensaje = 1
} )
alert.addAction(botonDos)
alert.addAction(botonUno)
present(alert, animated: true, completion: nil)
}
有什么建议吗?
最佳答案
显示警报 Controller 的方法是异步工作的。您不能在调用方法后立即同步处理结果。
有几种解决方案,其中之一是添加完成处理程序:
func crearAlertaDoble(titulo: String, mensaje: String, completion:@escaping (Int) ->()) {
let alert = UIAlertController(title: titulo, message: mensaje, preferredStyle: .alert)
let botonUno = UIAlertAction(title: "NO!", style: .destructive, handler: { action in
completion(0)
} )
let botonDos = UIAlertAction(title: "Si", style: .default, handler: { action in
completion(1)
} )
alert.addAction(botonDos)
alert.addAction(botonUno)
present(alert, animated: true, completion: nil)
}
并在 tableView:commit:forRowAt:
中使用它
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
crearAlertaDoble(titulo: "¿Seguro que deseas eliminar este calendario?", mensaje: "") { result in
print("opcion elegida: \(result)")
if result == 1 {
self.objetoContenedor.calendarios.remove(at: indexPath.row) //WIP, MOSTRAR MENSAJE SI ESTA SEGURO
self.tableView.deleteRows(at: [indexPath], with: .fade)
} else {
}
}
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
关于ios - 在删除一行 UITableView 之前发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43833765/