使用editActions编辑UITableView行并执行Segue?

时间:2016-11-21 11:33:17

标签: ios swift uitableview uistoryboardsegue

我尝试在TableView中添加一个滑动来编辑选项。如果用户按下编辑,则应打开一个新视图。

但是我总是在“editActionsForRowAt”的func“editActionsForRowAt”中遇到错误“if segue.identifier ==”ItemDetailsVS {“”“:致命错误:在解开可选值时意外发现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]

}

1 个答案:

答案 0 :(得分:0)

这是因为您正在使用未初始化的Segue对象使用“?”而不是“!”。同时检查标识符。

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html