I have a NavigationView that contains a UItableView at the bottom. At the top, there are some more views. I am using UItableView because I need both of its trailing and leading swipe actions.
Since the table view is a subview of NavigationView, is there any way to access it in UITableView, in its didSelectRowAt method?
Since the table view is a subview of NavigationView, is there any way to access it in UITableView, in its didSelectRowAt method?
Code Block 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
Code Block 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) } 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
Code Block struct ContentView: View { var body: some View { NavigationView { VStack { HStack { Text("Some other Views") } TableView() } } } }