ios - 使用 editActions 和 PerformSegue 编辑 UITableView 行?

标签 ios swift uitableview uistoryboardsegue

我尝试在 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 对象未初始化,因此请使用“?”而不是“!”。还要检查标识符。

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

关于ios - 使用 editActions 和 PerformSegue 编辑 UITableView 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40719152/

相关文章:

滚动时 iPhone UITableView 生涩

iphone - 添加单元格后,iOS UITableView 不滚动

ios - 为测试人员存档时,如何将 .entitlements 中的 `APS Environment` 值设置为 `development`?

ios - 带计时器的绘图应用程序。绘制后计时器开始滞后不到 20 秒

ios - NSFetchedResultsController - 用户驱动的更改

ios - Swift 中的 NSURL 问题

ios - 在 Button Swift 中通过标签发送行和部分

ios - 如何声明新数组来附加元组数组?

ios - 无法使用自动布局将简单的自调整大小 Collection View 单元格调整为标签大小

ios - 使用正确的身份验证数据时 Firebase 权限被拒绝(显然)