【问题标题】:How to provide custom header/footer to a tableView using RxDatasources如何使用 RxDatasources 为 tableView 提供自定义页眉/页脚
【发布时间】:2022-01-26 21:33:29
【问题描述】:

我真的无法在文档或其他地方找到它,但有没有办法使用 RxDatasources 提供从 nib 加载的自定义页眉和页脚?

例如,我将这样的单元格出列:

let dataSource = RxTableViewSectionedAnimatedDataSource<CommentsSectionModel>(
                configureCell: { dataSource, tableView, indexPath, item in
                    if let cell = tableView.dequeueReusableCell(withIdentifier: 
                       item.cellIdentifier, for: indexPath) as? BaseTableViewCell{
                       cell.setup(data: item.model)               
                       return cell
                    }
                return UITableViewCell()
 })

我没有看到与configureCelltitleForHeaderInSection 除外)一起让我出列/配置可重用页眉/页脚的东西(标准viewForHeaderInSectionviewForFooterInSection 委托方法提供的东西)。

【问题讨论】:

标签: ios swift rx-swift rxdatasources


【解决方案1】:

自定义的 Header/Footer 不是 UITableViewDataSource 接口的一部分,因此它不是 RxDataSource 可以提供的。

如果你愿意,你可以按照我关于如何Convert a Swift Delegate to RxSwift Observables 的文章并为此创建一个表视图委托...它不是库的一部分,因为表视图委托不符合推送接口。

extension Reactive where Base: UITableView {
    var delegate: UITableViewDelegateProxy {
        return UITableViewDelegateProxy.proxy(for: base)
    }

    var viewForHeaderInSection: Binder<[Int: UIView]> {
        Binder(delegate) { del, value in
            del.viewForHeaderInSection.accept(value)
        }
    }

    var viewForFooterInSection: Binder<[Int: UIView]> {
        Binder(delegate) { del, value in
            del.viewForFooterInSection.accept(value)
        }
    }
}

final class UITableViewDelegateProxy
: DelegateProxy<UITableView, UITableViewDelegate>
, DelegateProxyType
, UITableViewDelegate {

    public static func registerKnownImplementations() {
        self.register { UITableViewDelegateProxy(parentObject: $0) }
    }

    static func currentDelegate(for object: UITableView) -> UITableViewDelegate? {
        object.delegate
    }

    static func setCurrentDelegate(_ delegate: UITableViewDelegate?, to object: UITableView) {
        object.delegate = delegate
    }

    init(parentObject: UITableView) {
        super.init(
            parentObject: parentObject,
            delegateProxy: UITableViewDelegateProxy.self
        )
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        viewForHeaderInSection.value[section]
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        viewForFooterInSection.value[section]
    }

    fileprivate let viewForHeaderInSection = BehaviorRelay<[Int: UIView]>(value: [:])
    fileprivate let viewForFooterInSection = BehaviorRelay<[Int: UIView]>(value: [:])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 2011-12-27
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多