我用 UITableViewRowAction 创建了 UITableView:编辑和删除。按下删除时,确认应显示为 UIAlertController。如果用户按下删除,单元格应该被删除,如果用户按下取消,单元格应该被删除并且 UIAlertController 应该消失。我写了这段代码,但它没有按预期工作。
let edit = UITableViewRowAction(style: .normal, title: "Изменить") { action, index in
self.performSegue(withIdentifier: "LEDChangesSegue", sender: self.tableView.cellForRow(at: indexPath))
}
edit.backgroundColor = UIColor.init(red: 0, green: 122.0/256.0, blue: 1, alpha: 1)
let delete = UITableViewRowAction(style: .normal, title: "Удалить") { action, index in
let alertController = UIAlertController(title: self.ledControllers[indexPath.row].name, message: "Действительно удалить этот контроллер?", preferredStyle: .alert)
var answer = true
let cancelAction = UIAlertAction(title: "Отмена", style: .cancel) { (action:UIAlertAction!) in answer = false
}
let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in answer = true
}
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
if answer {
self.ledControllers.remove(at: index.row)
saveLED(self.ledControllers)
self.tableView.deleteRows(at: [index], with: .fade)
}}
delete.backgroundColor = UIColor.red()
return [edit, delete]
总是返回原始值var answer = true
,而不是我在 UIAlertController 中单击的值。什么修复代码以使其正常工作?