簡體   English   中英

如何使用 RxDatasources 為 tableView 提供自定義頁眉/頁腳

[英]How to provide custom header/footer to a tableView using RxDatasources

我真的無法在文檔或其他地方找到它,但是有沒有辦法使用 RxDatasources 提供自定義 header 和從 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委托方法提供的東西)。

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

如果你願意,你可以按照我關於如何將 Swift 委托轉換為 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: [:])
}

@DanielT 上面的回答適用於我的案例,但它只顯示 1 個部分的頁眉/頁腳。 我像這樣更新它並且它是固定的:

var viewForHeaderInSection: Binder<[Int: UIView]> {
        Binder(delegate) { del, value in
            let newValue = del.viewForHeaderInSection.value.merging(value, uniquingKeysWith: {(_,new) in new})
            del.viewForHeaderInSection.accept(newValue)
        }
    }

    var viewForFooterInSection: Binder<[Int: UIView]> {
        Binder(delegate) { del, value in
            let newValue = del.viewForFooterInSection.value.merging(value, uniquingKeysWith: {(_,new) in new})
            del.viewForFooterInSection.accept(newValue)
        }
    }
   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM