ios - 在传递数据之前在 tableview 中丢失行 id

标签 ios swift uitableview

我有一个嵌入到名为 UserDashboardVC 的 VC 中的导航 Controller 。然后我有一个菜单 VC,一个选项打开另一个带有表格 View 的 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 在我的单元格行被识别之前被执行?如何传回我的数据?

最佳答案

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

手动转场

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

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

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

关于ios - 在传递数据之前在 tableview 中丢失行 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118872/

相关文章:

iphone - 查看iPhone设备内的应用程序文件夹

ios - 核心蓝牙: Find out if device is available for reconnection

xcode - TableView 与 Xib 文件的协议(protocol) UITableViewDataSource 的冗余一致性

ios - 需要帮助设置数据模型以显示 tableView

ios - 从 'FIRRemoteConfigValue!' 转换为无关类型 'String' 总是失败

ios - 使用 Enum + Switch 进行错误处理时,如何显示不同的警报 View 功能?

单击后 Swift 3 立即隐藏 NSButton

ios - 两个类的 Swift 可选继承

ios - 如何仅为某些 UIViewControllers 启用前台通知?

ios - UITableViewCell 在滚动后被复制