首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Combine DataSource绑定UITableView

使用Combine DataSource绑定UITableView
EN

Stack Overflow用户
提问于 2021-04-07 18:11:28
回答 1查看 1.2K关注 0票数 1

我想不使用DiffableDataSouce直接链接一个带有@ UITableView属性的发布。

如果我让那个人

代码语言:javascript
复制
struct Person {
    let name: String
}

并创建数据数组:

代码语言:javascript
复制
@Published
var people = [Person(name: "Kim"), Person(name: "Charles")]

所以我想直接绑定我的UITableView,如下所示:

代码语言:javascript
复制
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'

EN

回答 1

Stack Overflow用户

发布于 2021-04-09 05:34:10

这里的问题是UITableViewDataSource是基于pull的(框架从您的代码中提取数据),而Publisher是基于push的(他们将数据推送到某个地方)。这意味着为了使其正常工作,您需要一个中介者(类似于中介者模式)。

一种选择是引入RxSwift/RxCocoa和RxCombine项目来在Combine和RxSwift之间进行转换,并使用已经存在的功能。对于这个问题来说,这是一个很大的要求,但也许您还有其他方面可以让RxCocoa简化您的代码。

对于这个问题,这里有一个我认为可以工作的Mediator:

代码语言:javascript
复制
@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])
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66983922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档