gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:53:09 25 4
gpt4 key购买 nike

我尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com