ios - Xcode:将信息从 UITableViewController 传递到 UIViewController

标签 ios swift xcode uitableview uiviewcontroller

我有一个 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(带有最后的信息)。

有没有更好的办法解决这个问题?

最佳答案

尝试使用 indexPathForSelectedRowprepareForSegue看起来你已经从 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/

相关文章:

iphone - 使用 xcode 中的 validate 验证应用程序 - 发现错误 - 提供图像

ios - 更改 sectionNameKeyPath 属性项属性时 NSFetchedResultsController 崩溃

ios - 如何在 Xcode 中存档/导出框架?

swift - 使用 updateApplicationContext 将自定义对象从 Apple Watch 传输到 iPhone

ios - 如何在 Swift 中拍摄移动的 Sprite ?

swift - 符合协议(protocol)——一次又一次地添加协议(protocol) stub 并不能修复错误

c++ - 从 CIImage 创建垫子的最佳方法?

javascript - 网站导航在 WKWebview 中不起作用

objective-c - 安装后 Xcode 4.0.2 未启动

ios - 如何获取放置在 tableviewcell 中的图像的原点(在屏幕中)?