繁体   English   中英

SwiftUI - 从 UITableView (UIViewRepresentable) 导航到 View

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

我有一个NavigationView ,底部包含一个UItableView 在顶部,还有一些视图。 我正在使用UItableView因为我需要它的尾随和前导滑动动作。

由于表格视图是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

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])
        }
    }
}

包含 tableView 的视图

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

使用这种方式或其他方式,您可以获得didSelectRowAt方法

表视图

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])
        }
    }
}

内容视图

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM