0

我有一个UIViewController包装的 via UIViewControllerRepresentable。在我的控制器中是一个UITableView. 这是通过自定义演示文稿显示的。当用户点击其中一个单元格时,SwiftUI 视图将更新。现在,当用户再次展示包装好的控制器时,我希望UITableView滚动到选定的行,以便将其放置在顶部,而不是始终在顶部具有索引 0。

这是我尝试过的:

struct TableDropdownView: UIViewControllerRepresentable {
    typealias UIViewControllerType = dropdownViewController

    // The index the table should scroll to.
    let indexToScroll: Int
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<TableDropdownView>) -> dropdownViewController {
        let vc = dropdownViewController()
        vc.currentIndex = indexToScroll
        return vc
    }

    func updateUIViewController(_ uiViewController: dropdownViewController, context: UIViewControllerRepresentableContext<TableDropdownView>) {
        // This is what I have tried but it doesn't work.
        uiViewController.myTableView.scrollToRow(at: IndexPath(row: indexToScroll, section: 0), at: .top, animated: false)
    }
    
    func makeCoordinator() -> TableDropdownView.Coordinator {
        return Coordinator(self)
    }

    final class Coordinator {
        var contentView: TableDropdownView
        init(_ contentView: TableDropdownView) {
            self.contentView = contentView
        }
    }
}
4

0 回答 0