简体   繁体   English

SwiftUI - 从 UITableView (UIViewRepresentable) 导航到 View

[英]SwiftUI - Navigate from UITableView (UIViewRepresentable) to View

I have a NavigationView that contains a UItableView at the bottom.我有一个NavigationView ,底部包含一个UItableView At the top, there are some more views.在顶部,还有一些视图。 I am using UItableView because I need both of its trailing and leading swipe actions.我正在使用UItableView因为我需要它的尾随和前导滑动动作。

Since the table view is a subview of NavigationView , is there any way to access it in UITableView , in its didSelectRowAt method?由于表格视图是NavigationView的子视图,有什么方法可以在UITableViewdidSelectRowAt方法中访问它?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let destination = Text("Destination")
        let host = UIHostingController(rootView: destination)
        
        //TODO: - Navigate to Destination
        //navigationController.pushViewController(host, animated: true)
    }

UITableView UITableView

struct TableView: UIViewRepresentable {
    @State var rows = ["London", "Paris", "Oslo"]
    
    func makeUIView(context: Context) -> UITableView {
        let table = UITableView()
        table.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        table.dataSource = context.coordinator
        table.delegate = context.coordinator
        return table
    }
    
    func updateUIView(_ uiView: UITableView, context: Context) {
        
    }
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(rows: $rows)
    }
    
    class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
        @Binding var rows: [String]
        
        init(rows: Binding<[String]>) {
            self._rows = rows
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.rows.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
            cell?.textLabel?.text =  self.rows[indexPath.row]
            return cell!
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let destination = Text("Destination")
            let host = UIHostingController(rootView: destination)
            
            //TODO: - Navigate to Destination
            //navigationController.pushViewController(host, animated: true)
        }
        
        func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt  indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let add = UIContextualAction(style: .normal,title: "Add") { (action, view, success) in
                success(true)
            }
            
            add.backgroundColor = .gray
        
            
            return UISwipeActionsConfiguration(actions:  [add])
        }
        func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let remove = UIContextualAction(style: .normal,title: "Remove") { (action, view, success) in
                success(true)
            }
            
            remove.backgroundColor = .red
            
            let edit = UIContextualAction(style: .normal,title: "Edit") { (action, view, success) in
                success(true)
            }
            
            edit.backgroundColor = .gray
            
            
            let color = UIContextualAction(style: .normal, title: "Color") { (action, view, success) in
                success(true)
            }
            
            return UISwipeActionsConfiguration(actions:  [remove, edit, color])
        }
    }
}

View that contains the tableView包含 tableView 的视图

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                HStack {
                    Text("Some other Views")
                }
                TableView()
            }
        }
    }
}

Using this way or other way, you can get didSelectRowAt method使用这种方式或其他方式,您可以获得didSelectRowAt方法

Tableview表视图

struct TableView: UIViewRepresentable {
    
    var completionHandler:((String) -> Void) = {_ in }
    
    @State var rows = ["London", "Paris", "Oslo"]
    
    func makeUIView(context: Context) -> UITableView {
        let table = UITableView()
        table.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        table.dataSource = context.coordinator
        table.delegate = context.coordinator
        return table
    }
    
    func updateUIView(_ uiView: UITableView, context: Context) {
        
    }
    
    func makeCoordinator() -> Coordinator {
        //return Coordinator(rows: $rows)
        return Coordinator(self)
    }
    
    class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
        //@Binding var rows: [String]
        
        @State var parent: TableView
        
        init(_ parent: TableView){
            self.parent = parent
        }
        
        /*
        init(rows: Binding<[String]>) {
            self._rows = rows
        }*/
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.parent.rows.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
            cell?.textLabel?.text =  self.parent.rows[indexPath.row]
            return cell!
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.parent.completionHandler(self.parent.rows[indexPath.row])
            //let host = UIHostingController(rootView: destination)
            
            //TODO: - Navigate to Destination
            //navigationController.pushViewController(host, animated: true)
        }
        
        func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt  indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let add = UIContextualAction(style: .normal,title: "Add") { (action, view, success) in
                success(true)
            }
            
            add.backgroundColor = .gray
        
            
            return UISwipeActionsConfiguration(actions:  [add])
        }
        func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let remove = UIContextualAction(style: .normal,title: "Remove") { (action, view, success) in
                success(true)
            }
            
            remove.backgroundColor = .red
            
            let edit = UIContextualAction(style: .normal,title: "Edit") { (action, view, success) in
                success(true)
            }
            
            edit.backgroundColor = .gray
            
            
            let color = UIContextualAction(style: .normal, title: "Color") { (action, view, success) in
                success(true)
            }
            
            return UISwipeActionsConfiguration(actions:  [remove, edit, color])
        }
    }
}

ContentView内容视图

struct ContentView: View {

@State var selectedString: String = ""
@State var isPresent: Bool = false
var body: some View {
    NavigationView {
        VStack {
            HStack {
                Text("Some other Views")
            }
TableView(completionHandler: { (s) in
             self.selectedString = s
             self.isPresent = true
             
            }, rows: ["A","B","C","D"])
            
            NavigationLink(destination: Text(self.selectedString), isActive: $isPresent) {
                EmptyView()
            }
            /*TableView(completionHandler: { str in
                self.selectedString = str
                self.isPresent = true
            }, rows: ["A","B","C","D"])*/
        }
        
        .sheet(isPresented: self.$isPresent) {
            Text(self.selectedString)
        }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI:如何从 UIViewRepresentable 导航到 SwiftUI 视图 - SwiftUI: How to navigate from UIViewRepresentable to SwiftUI View 从 SwiftUI 中的 UIViewRepresentable 呈现视图 - Present view from UIViewRepresentable in SwiftUI UITableView 与 UIViewRepresentable 在 SwiftUI - UITableView with UIViewRepresentable in SwiftUI UIViewRepresentable 隐藏在 SwiftUI 视图后面 - UIViewRepresentable hidden behind SwiftUI View SwiftUI:从带有导航返回按钮的视图导航到全屏视图 - SwiftUI: Navigate from a view with a navigationbackbutton to a fullscreen view 如何从 SwiftUI 视图导航回 UIViewController? - How to navigate back to UIViewController from SwiftUI View? 导航到 SwiftUI 中的相同视图 - Navigate to same view in SwiftUI 如何将 SwiftUI 视图放置在 UIKit (UIViewRepresentable) 视图之上? (错误或错误?) - How to place a SwiftUI view on top of a UIKit (UIViewRepresentable) view? (bug or mistake?) 带有 UIViewRepresentable 的 SwiftUI 自定义文本字段 ObservableObject 和推送视图的问题 - SwiftUI Custom TextField with UIViewRepresentable Issue with ObservableObject and pushed View SwiftUI - 使用 UIViewRepresentable 包装视图以使用 NSMutableAttributedString - SwiftUI - Using UIViewRepresentable to wrap view in order to use NSMutableAttributedString
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM