【问题标题】:How to get the right row in a @IBsegueAction function?如何在@IBsegueAction 函数中获得正确的行?
【发布时间】:2019-07-24 17:21:12
【问题描述】:

我可以使用必要的数据从嵌入的UIKit tableview 转到SwiftUI 视图。我选择indexPath.rowtableView(_didSelectRowAt)。 但是,@IBSegueAction 发生在 didSelectRowAt 之前。这使得 detailView 滞后一个选定的行:它显示先前选定的行。

我试图把 didSelectRowAt 首先,试图嵌入它们:没有机会 我在 WWDC 视频中看到应该可以选择正确的行,但无法从这个短片段中找出正确的语法(大约 6:00 分钟) https://developer.apple.com/videos/play/wwdc2019/231/

@IBSegueAction func MeasurementDetail(_ coder: NSCoder) -> UIViewController? {
    return UIHostingController(coder: coder, rootView: PointDetailSwiftUIView(pointDetail: measurements[selectedMeasurementRow]))
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedMeasurementRow = indexPath.row
    }

我该如何解决这个问题?

【问题讨论】:

    标签: uikit swiftui


    【解决方案1】:

    在 WWDC 会话中,Tanu 不在乎 UIKit 何时调用 tableView(_:didSelectRowAt:)。相反,她的@IBSegueAction 使用tableView.indexPathForSelectedRow 向表格视图询问所选行。

    确保您有一个tableView 插座连接到您的表格视图。如果您的视图控制器是UITableViewController 的子类,那么您已经有一个tableView 出口。然后使用tableView.indexPathForSelectedRow:

    @IBSegueAction func MeasurementDetail(_ coder: NSCoder) -> UIViewController? {
        guard let row = tableView.indexPathForSelectedRow?.row else { return nil }
        let detailView = PointDetailSwiftUIView(pointDetail: measurements[row])
        return UIHostingController(coder: coder, rootView: detailView)
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 2012-11-06
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2021-04-11
      相关资源
      最近更新 更多