I just created a UIKit tableView with UIViewRepresentable
.
Now I need to present a new SwiftUI View, if a tableCell is selected.
This is what I've done so far:
struct UIKitTableView: UIViewRepresentable {
@Binding var showDetail: Bool
func makeCoordinator() -> Coordinator {
Coordinator(showDetail: self.$showDetail)
}
class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
@Binding var showDetail: Bool
/// Did select row at
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.showDetail = true
}
Now in my SwiftUI view I can present the view with:
NavigationLink(destination: CellDetail(), isActive: self.$showDetail) {
But I also need to pass the data of the corrispective cell, and I was wondering which is the best approach.
My other question is: Is the @Binding showDetail approach correct? How can I improve my solution?
According to the situation, you're describing I don't understand why you would need to use UIViewRepresentable
, since SwiftUI already have a very good implementation of it under the name of List
Here is a piece of code to help you show detail view:
import SwiftUI
struct MasterView: View {
private let cells = [
"Cell 1", "Cell 2", "Cell 3"
]
var body: some View {
NavigationView {
List(cells, id: \.self) { cell in
NavigationLink(destination: DetailsView(content: cell)) { // Creates all the logic to show detail view.
Text(cell)
}
}.navigationBarTitle("List")
}
}
}
struct DetailsView: View {
let content: String
var body: some View {
VStack {
Text(content)
.font(.largeTitle)
}
}
}
Keep in mind that UIViewRepresentable
only helps when a view isn't implemented yet in SwiftUI.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.