3

如何使用 Bond 框架向我的 UITableViewCell 添加部分?

 self.viewModel.items.bind(to: self.tableView) { (item, indexPath, tableView) -> UITableViewCell in
                      let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ListCell.self),
                                                               for: indexPath) as! ListCell
                       cell.item = item[indexPath.row]
                      return cell
                  }.dispose(in: self.bag)
4

2 回答 2

0

关于源代码,

如果你只想覆盖一个标题,你应该覆盖这个类并实现相应的逻辑

open class TableViewBinderDataSource<Changeset: SectionedDataSourceChangeset>: NSObject, UITableViewDataSource

但是如果你想实现完全自定义的视图,这就很复杂了。我认为这个图书馆不可能。原因是你应该覆盖UITableViewDelegate,但它用于public protocol ReactiveExtensions不能被覆盖。

于 2019-11-06T22:11:58.133 回答
0

你必须写这个类

class  CustomSection<Changeset: SectionedDataSourceChangeset>: TableViewBinderDataSource<Changeset>, UITableViewDelegate where Changeset.Collection == Array2D <String, ListItemViewModel> {

        @objc func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            return changeset?.collection[sectionAt: section].metadata
        }

在 ViewController 的 viewDidload 中,您必须调用此函数。

private func setupViewModel() {  
        let sectionBindingDatSource: CustomSection = CustomSection<TreeChangeset>{ (changeset, indexPath, tableView) -> UITableViewCell in
            let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ListCell.self), for: indexPath) as! ListCell
            cell.item = changeset.sections[indexPath.section].items[indexPath.row]

            return cell
        }
        self.viewModel.sections.bind(to: self.tableView, using: sectionBindingDatSource)
    }

如果要覆盖 TableViewDataSourse 的功能并自定义部分,则必须设置委托

self.tableView.delegate = sectionBindingDatSource
于 2019-11-08T14:28:54.563 回答