UITableViewRowAction: edit and delete で 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 でクリックしたものではなく、常に元の値を返します。正しく動作するようにコードを修正するにはどうすればよいですか?