我想不使用DiffableDataSouce直接链接一个带有@ UITableView属性的发布。
如果我让那个人
struct Person {
let name: String
}并创建数据数组:
@Published
var people = [Person(name: "Kim"), Person(name: "Charles")]所以我想直接绑定我的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'
发布于 2021-04-09 05:34:10
这里的问题是UITableViewDataSource是基于pull的(框架从您的代码中提取数据),而Publisher是基于push的(他们将数据推送到某个地方)。这意味着为了使其正常工作,您需要一个中介者(类似于中介者模式)。
一种选择是引入RxSwift/RxCocoa和RxCombine项目来在Combine和RxSwift之间进行转换,并使用已经存在的功能。对于这个问题来说,这是一个很大的要求,但也许您还有其他方面可以让RxCocoa简化您的代码。
对于这个问题,这里有一个我认为可以工作的Mediator:
@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])
}
}https://stackoverflow.com/questions/66983922
复制相似问题