Xcode:将信息从UITableViewController传递到UIViewController [英] Xcode: Passing Information from UITableViewController to UIViewController

查看:111
本文介绍了Xcode:将信息从UITableViewController传递到UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController应该根据在UITableViewController中按下的Cell来显示DetailInformations。

I have a UIViewController which should show me DetailInformations depending on what Cell was pressed in the UITableViewController.

目前我正在通过续集传递它们:

For the moment I am passing them through a sequel:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "show" {
            var ctrl = segue.destination as! DetailViewController
            ctrl.information = _informationList[id]
        }
    }

id变量通过以下方式设置:

The id variable is set through:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        id = indexPath.row

    }

现在在我的UIViewController中我更改信息:

Now in my UIViewController I change the information with:

override func viewDidLoad() {
        super.viewDidLoad()
        setInformation(i: information)
    }

现在我的问题是,如果我按,就说单元格2.它切换到ViewController并显示单元格1的信息。然后我回到tableview并按下单元格3.然后它显示单元格2.

Now my problem is, that if I press, lets say cell 2. It switches to the ViewController and shows Information of cell 1. Than I go back to the tableview and I press cell 3. Then it shows me cell 2.

简而言之,似乎在设置新信息之前加载了viewController(带有最后的信息)。

In short, it seems that the viewController is loaded (with the last information), before it sets the new information.

有没有更好的方法可以解决这个问题?

Is there any better way to solve this?

推荐答案

尝试在 prepareForSegue 中使用 indexPathForSelectedRow ,看起来你已经从 UITableViewCell创建了segue 到目的地 ViewController ,以便 prepareForSegue 将在之前调用didSelectRowAt

Try using indexPathForSelectedRow in prepareForSegue as of it looks like that you have created segue from UITableViewCell to the Destination ViewController so that prepareForSegue will call before the 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]
        }
    }
}

这篇关于Xcode:将信息从UITableViewController传递到UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆