我已经用UITableView :编辑和删除创建了UITableViewRowAction。当按下删除时,确认应该显示为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中单击的内容。是什么修复了代码才能使它正确工作?
发布于 2016-06-30 16:56:44
解决方案1
您可以直接这样做,而不必接受变量answer,因为这是局部变量,并且在此块之外没有作用域。
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)
let cancelAction = UIAlertAction(title: "Отмена", style: .cancel) { (action:UIAlertAction!) in
}
let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in
self.ledControllers.remove(at: index.row)
self.saveLED(self.ledControllers)
self.tableView.deleteRows(at: [index], with: .fade)
}
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
delete.backgroundColor = UIColor.red()
return [edit, delete]解决方案2
如果您真的想用变量answer(实际上没有意义使用)来实现这一点,那么编写一个单独的函数来删除该行。
func deleteRowNow(answer : Bool, forRow : NSIndexPath)
{
if answer {
self.ledControllers.remove(at: index.row)
saveLED(self.ledControllers)
self.tableView.deleteRows(at: [index], with: .fade)
}}
}并在UIAlertAction块内调用它
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
self.deleteRowNow(answer, forRow : index)
}
let deleteAction = UIAlertAction(title: "Удалить", style: .destructive ) { (action:UIAlertAction!) in
answer = true
self.deleteRowNow(answer, forRow : index)
}
alertController.addAction(deleteAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
delete.backgroundColor = UIColor.red()
return [edit, delete]https://stackoverflow.com/questions/38120459
复制相似问题