gpt4 book ai didi

swift - 将 UITableView 与 Combine DataSource 绑定(bind)

转载 作者:行者123 更新时间:2023-12-02 18:49:27 30 4
gpt4 key购买 nike

我想直接链接一个UITableView使用 @Published 属性而不使用 DiffableDataSouce。

如果我做人

struct Person {
let name: String
}

并创建数据数组:

@Published
var people = [Person(name: "Kim"), Person(name: "Charles")]

所以我想绑定(bind)我的 UITableView直接,用类似的东西:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return $people.count()
}

但这给出了错误

Cannot convert return expression of type 'Publishers.Count<Published[Person]>.Publisher>' to return type 'Int'

最佳答案

这里的问题是 UITableViewDataSource 是基于拉取的(框架从您的代码中提取数据),但发布者是基于推送的(它们将数据推送到某个东西。)这意味着为了使其工作,您需要一个调解器(类似于中介者模式。)

一种选择是引入 RxSwift/RxCocoa 和 RxCombine 项目以在 Combine 和 RxSwift 之间进行转换,并使用已经存在的功能。这个问题太多了,但也许你还有其他领域 RxCocoa 可以简化你的代码。

对于这个问题,这里有一个我认为可行的调解器:

@available(iOS 13.0, *)
final class ViewController: UIViewController {

var tableView: UITableView = UITableView()
@Published var people = [Person(name: "Kim"), Person(name: "Charles")]
var cancellable: AnyCancellable?

override func viewDidLoad() {
super.viewDidLoad()

tableView.frame = view.bounds
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(tableView)

cancellable = $people.sink(receiveValue: tableView.items { tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = item.name
return cell
})

DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
self.people = [Person(name: "Mark"), Person(name: "Allison"), Person(name: "Harold")]
}
}
}

extension UITableView {
func items<Element>(_ builder: @escaping (UITableView, IndexPath, Element) -> UITableViewCell) -> ([Element]) -> Void {
let dataSource = CombineTableViewDataSource(builder: builder)
return { items in
dataSource.pushElements(items, to: self)
}
}
}

class CombineTableViewDataSource<Element>: NSObject, UITableViewDataSource {

let build: (UITableView, IndexPath, Element) -> UITableViewCell
var elements: [Element] = []

init(builder: @escaping (UITableView, IndexPath, Element) -> UITableViewCell) {
build = builder
super.init()
}

func pushElements(_ elements: [Element], to tableView: UITableView) {
tableView.dataSource = self
self.elements = elements
tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
elements.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
build(tableView, indexPath, elements[indexPath.row])
}
}

关于swift - 将 UITableView 与 Combine DataSource 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66983922/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com