swift - 如何使用 SwiftUI 视图代替表格视图单元格
问题描述
如何使用 SwiftUI 视图结构代替 UITableViewController 中的传统单元格和 xib?
import UIKit
import SwiftUI
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var objects = [Any]()
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.title = "Table View"
//...
}
// MARK: - Table View Methods
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(MySwiftUIView())
// ...
return cell
}
} ...
这个问题很明显,因为 UIHostedController SwiftUI 视图不是表格单元格,但我怎么能像使用它一样使用它呢?
解决方案
感谢您在这里回答您自己的问题。您的解决方案帮助我制作了一个通用的 HostingTableViewCell 类。如果有人像我一样在 Google 上发现这个问题,我会在这里发布。
import SwiftUI
class HostingTableViewCell<Content: View>: UITableViewCell {
private weak var controller: UIHostingController<Content>?
func host(_ view: Content, parent: UIViewController) {
if let controller = controller {
controller.rootView = view
controller.view.layoutIfNeeded()
} else {
let swiftUICellViewController = UIHostingController(rootView: view)
controller = swiftUICellViewController
swiftUICellViewController.view.backgroundColor = .clear
layoutIfNeeded()
parent.addChild(swiftUICellViewController)
contentView.addSubview(swiftUICellViewController.view)
swiftUICellViewController.view.translatesAutoresizingMaskIntoConstraints = false
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 0.0))
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: 0.0))
swiftUICellViewController.didMove(toParent: parent)
swiftUICellViewController.view.layoutIfNeeded()
}
}
}
在你的 UITableViewController 中:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(HostingTableViewCell<Text>.self, forCellReuseIdentifier: "textCell")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "textCell") as! HostingTableViewCell<Text>
cell.host(Text("Yay!"), parent: self)
return cell
}
如果人们似乎使用它,可能会将其变成一个包。
推荐阅读
- sql - SQL中如何根据涉及多个日期的不同条件选择结果?
- dataframe - VarcharType 不匹配 Spark 数据框
- android - 在 android 5 (API 21) 上忽略了 clipToPadding 属性
- powershell - 选项去哪儿了?
- php - 如何订购“斑马”数组,以便每个键都有一个备用值(1、0)
- javascript - 在控制台中记录 performance.now()
- angular - 重新触发服务调用以重复异步请求
- python - 我可以为我的 tornado.testing.AsyncTestCase 测试提供一个抽象基类吗?
- javascript - 函数Javascript中的变量不变
- css - 我们可以将默认的角材料芯片设计更改为矩形吗?