我尝试在 TableView 中添加滑动编辑选项。如果用户按“编辑”,则应打开一个新 View 。
但是我总是在 func“editActionsForRowAt”中遇到错误“if segue.identifier ==”ItemDetailsVS {“”“: fatal error :在展开可选值时意外发现 nil
segue 在 Storyboard 中具有正确的名称。有什么想法吗?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ItemDetailsVC" {
if let destination = segue.destination as? ItemDetailsViewController {
if let item = sender as? Item2 {
destination.itemToEdit = item
}
}
}
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let edit = UITableViewRowAction(style: .normal, title: "Edit"){ (action, indexPath) in
var segue: UIStoryboardSegue!
if segue.identifier == "ItemDetailsVC" {
if let objects = self.controller.fetchedObjects , objects.count > 0 {
let item = objects[indexPath.row]
self.performSegue(withIdentifier: "ItemDetailsVC", sender: item)
}
}
}
return [edit]
}
最佳答案
这是因为您使用的 Segue 对象未初始化,因此请使用“?”而不是“!”。还要检查标识符。
关于ios - 使用 editActions 和 PerformSegue 编辑 UITableView 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40719152/