[英]SwiftUI - Navigate from UITableView (UIViewRepresentable) to View
[英]Present view from UIViewRepresentable in SwiftUI
我剛剛用UIViewRepresentable
創建了一個 UIKit tableView。
現在我需要呈現一個新的 SwiftUI 視圖,如果選擇了一個 tableCell。
這是我到目前為止所做的:
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
}
現在在我的 SwiftUI 視圖中,我可以通過以下方式呈現視圖:
NavigationLink(destination: CellDetail(), isActive: self.$showDetail) {
但我還需要傳遞相應單元格的數據,我想知道哪種方法最好。
我的另一個問題是:@Binding showDetail 方法是否正確? 我怎樣才能改進我的解決方案?
根據情況,你描述我不明白為什么你需要使用UIViewRepresentable
,因為 SwiftUI 已經以List
的名義很好地實現了它
這是一段代碼,可以幫助您顯示詳細信息視圖:
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)
}
}
}
請記住,只有在 SwiftUI 中尚未實現視圖時, UIViewRepresentable
才有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.