UITableView:单击一个单元格并触发一个新的SwiftUI视图



我有以下代码,有两件事我必须做,一旦选择了Cell(已经在做了(,要启动一个新视图,新视图必须是SwiftUI视图,我已经附上了当前应用程序运行的图像,任何帮助都将不胜感激,感谢

import UIKit
var rows = ["A", "B", "C", "D"]
class ViewController: UIViewController  {
let uiKitTableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
configUITableView()
self.uiKitTableView.register(UITableViewCell.self, forCellReuseIdentifier: "tablecell")
let indexPath = IndexPath(row: 0, section: 0)
self.uiKitTableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}
override func viewDidLayoutSubviews() {
super.viewWillLayoutSubviews()
uiKitTableView.frame = view.bounds
}
func configUITableView() {
view.addSubview(uiKitTableView)
uiKitTableView.delegate = self
uiKitTableView.dataSource = self
uiKitTableView.rowHeight = 50
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let uiTableViewCell = tableView.dequeueReusableCell(withIdentifier: "tablecell", for: indexPath)
uiTableViewCell.textLabel?.text = " (rows[indexPath.row])"
return uiTableViewCell
}
}

运行的打印屏幕应用程序

UITableViewdidSelectRowAt中,您可以使用以下代码行打开SwiftUI View

let swiftUIController = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(swiftUIController, animated: true)

别忘了import SwiftUI

最新更新