首页 > 解决方案 > 在传递数据之前在 tableview 中丢失行 id

问题描述

我有一个嵌入到名为 UserDashboardVC 的 VC 中的导航控制器。然后我有一个菜单 VC,一个选项打开另一个带有表格视图的 VC,ManageAccountVC。当我选择表格行时,我希望它展开并填充 UserDashboardVC 上的数据。

我正在努力使用来自 ManageAccountVC 的 unwind segue 将数据传递回 UserDashboardVC。

在我的 UserDashboardVC (root) 中,我有我的 unwind segue 代码:

@IBAction func unwindUserDashboardVC(_ unwindSegue: UIStoryboardSegue) {
    userCompanyLabel.text   = PassCompanyOffice}

在我的 ManageAccountVC 中,tableview 单元格已连接到 Exit unwindUserDashboardVC,当我单击单元格时,我将其展开到 UserDashboardVC。我在 ManageAccountVC 中有一个函数来选择行:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    PassCompanyOffice = userAccountArray[indexPath.row].companyOffice!
}

我在 ManageAccountVC 中也有 Prepare 函数,它似乎在我获得行值之前触发:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let ConfirmVC = segue.destination as! UserDashboardVC
        ConfirmVC.PassCompanyOffice = PassCompanyOffice
}

为什么我的 unwind segue 在我的单元格行被识别之前被执行?如何传回我的数据?

标签: iosswiftuitableview

解决方案


您可能希望创建一个类成员来保存选定的行并在 willSelectRow 中为 tableview 分配它。然后在准备 Segue 或 unwind segue 中获取值。在 unwind segue 中放置一个断点以确定所选行变量的状态,然后再使用它。

手动转场

要将单元格单击操作与 segue 分开,您需要删除从 tableview 单元格到出口的退出 segue。

然后创建从视图控制器到退出图标的手动转场。给那个 segue 一个标识符,然后用标识符调用 performSegue(使用 exitSegueIdentifier)

这样你就可以将这两个动作分开。您可以在不退出的情况下单击表格。在您的代码库中,您可以决定何时调用 performSegue 并使用手动退出 segue 实际关闭 VC。


推荐阅读