ios - SwiftUI - 从 UITableView (UIViewRepresentable) 导航到 View
问题描述
我有一个NavigationView ,底部包含一个UItableView 。在顶部,还有一些视图。我使用UItableView是因为我需要它的尾随和前导滑动操作。
由于表格视图是NavigationView的子视图,有什么方法可以在UITableView的didSelectRowAt方法中访问它?
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)
}
}
}
}
推荐阅读
- node.js - “darwin-arm64v8”二进制文件不能在“darwin-x64”平台上使用
- javascript - jquery - 选择器不会记录元素
- angular - 将图标添加到 datagrid 。我想添加一个视频图标
- java - 我需要一种可扩展的方法来增加 2 种布尔输入和输出的数量,比例为 inputA:inputB:output, 2:1:2, 3:2:3, 4:3:4,
- reactjs - 如何在 Redux 工具包中设置状态(!状态)
- javascript - 我如何通过名称 Vue.js 路由链接到子组件
- javascript - 摩纳哥编辑器:禁用命令调色板
- python - 相关的空中和地面照片
- java - 定期统计 Kafka 流中的消息/事件数
- postgresql - postgres 在表中创建唯一行