Swift UISwipeActionsConfiguration 和 segue
Swift UISwipeActionsConfiguration and segue
我添加了 leadingSwipeAction“编辑”按钮。但是,当我在模拟器中按下“编辑”按钮时,应用程序崩溃并在 **prepare(for:sender:)** 方法中显示“线程 1:信号 SIGBART”。
我看到了类似的问题,但他们的解决方案没有帮助。我是新手,不明白问题出在哪里。
`//my edit button code`
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let edit = UIContextualAction(style: .normal, title: "Edit") { [self] (contextualAction, view, actionPrformed: (Bool) -> Void) in
//TODO:
performSegue(withIdentifier: "EditItem", sender: self)
actionPrformed(true)
}
return UISwipeActionsConfiguration(actions: [edit])
}
// segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddItem" {
let controller = segue.destination as! AddAndEditItemViewController
controller.delegate = self
}
else if segue.identifier == "EditItem" {
let controller = segue.destination as! AddAndEditItemViewController
controller.delegate = self
if let indexPath = tableView.indexPath(
for: sender as! UITableViewCell) { //erorr shows here
controller.itemToEdit = items[indexPath.row]
}
}
}
self
这里
performSegue(withIdentifier: "EditItem", sender: self)
是vc实例本身不是cell,需要传indexPath.row
performSegue(withIdentifier: "EditItem", sender: indexPath.row)
然后
let index = sender as! Int
controller.itemToEdit = items[index]
您也可以使用
guard let index = tableView.indexPathForSelectedRow?.row else { return }
我添加了 leadingSwipeAction“编辑”按钮。但是,当我在模拟器中按下“编辑”按钮时,应用程序崩溃并在 **prepare(for:sender:)** 方法中显示“线程 1:信号 SIGBART”。
我看到了类似的问题,但他们的解决方案没有帮助。我是新手,不明白问题出在哪里。
`//my edit button code`
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let edit = UIContextualAction(style: .normal, title: "Edit") { [self] (contextualAction, view, actionPrformed: (Bool) -> Void) in
//TODO:
performSegue(withIdentifier: "EditItem", sender: self)
actionPrformed(true)
}
return UISwipeActionsConfiguration(actions: [edit])
}
// segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddItem" {
let controller = segue.destination as! AddAndEditItemViewController
controller.delegate = self
}
else if segue.identifier == "EditItem" {
let controller = segue.destination as! AddAndEditItemViewController
controller.delegate = self
if let indexPath = tableView.indexPath(
for: sender as! UITableViewCell) { //erorr shows here
controller.itemToEdit = items[indexPath.row]
}
}
}
self
这里
performSegue(withIdentifier: "EditItem", sender: self)
是vc实例本身不是cell,需要传indexPath.row
performSegue(withIdentifier: "EditItem", sender: indexPath.row)
然后
let index = sender as! Int
controller.itemToEdit = items[index]
您也可以使用
guard let index = tableView.indexPathForSelectedRow?.row else { return }