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

标签 swift combine

我想直接链接一个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/

相关文章:

swift - 将 SwiftUI 与自定义发布者结合起来 - 使用 .assign 订阅者时出现意外行为

swift - "some Protocol"导致类型不符合协议(protocol)

json - 如何使用组合从 SwiftUI 中的 URL 解析单个 JSON 对象?

iOS 10 - 模糊背景不再起作用

swift - 如何检测最后一个单元格在 UICollectionView 中是否可见?

swift - 如何从 SwiftUI 中带有修饰符的函数返回按钮?

swiftui - 组合 + SwiftUI Form + RunLoop 导致表格 View 呈现不可预测

swift - 触发 CombineLatest 在 Combine 中传播初始值

swift - 在 Swift 中将类型变量作为参数传递

ios - 表格单元格约束无法正确加载