我有一个 UIViewController,它应该根据在 UITableViewController 中按下的 Cell 向我显示 DetailInformations。
目前我正在通过续集传递给他们:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "show" {
var ctrl = segue.destination as! DetailViewController
ctrl.information = _informationList[id]
}
}
id 变量是通过以下方式设置的:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
id = indexPath.row
}
现在在我的 UIViewController 中,我将信息更改为:
override func viewDidLoad() {
super.viewDidLoad()
setInformation(i: information)
}
现在我的问题是,如果我按下,假设单元格 2。它切换到 ViewController 并显示单元格 1 的信息。然后我返回到 tableview 并按下单元格 3。然后它显示单元格 2。
简而言之,似乎在设置新信息之前加载了 viewController(带有最后的信息)。
有没有更好的办法解决这个问题?
最佳答案
尝试使用 indexPathForSelectedRow
在 prepareForSegue
看起来你已经从 UITableViewCell
创建了 segue到目的地ViewController
这样prepareForSegue
会在didSelectRowAt
之前打电话.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "show" {
var ctrl = segue.destination as! DetailViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
ctrl.information = _informationList[indexPath.row]
}
}
}
关于ios - Xcode:将信息从 UITableViewController 传递到 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589630/