我RxDatasources用來創建我的資料源。稍后,我在視圖控制器中配置單元格。問題是,因為 headers/footers 與資料源沒有任何關系(除了我們可以設定標題,但如果我們使用自定義頁眉頁腳,這個標題將被覆寫)。
現在,這就是我配置表格視圖單元格的方式:
private func observeDatasource(){
let dataSource = RxTableViewSectionedAnimatedDataSource<ConfigStatusSectionModel>(
configureCell: { dataSource, tableView, indexPath, item in
if let cell = tableView.dequeueReusableCell(withIdentifier: ConfigItemTableViewCell.identifier, for: indexPath) as? BaseTableViewCell{
cell.setup(data: item.model)
return cell
}
return UITableViewCell()
})
botConfigViewModel.sections
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
現在導致
dataSource.titleForHeaderInSection = { dataSource, index in
return dataSource.sectionModels[index].model
}
...不起作用,因為我想加載一個自定義標題并用來自的資料填充它RxDatasource,我想知道什么是正確的方法:
- 從我的視圖模型中定義的資料源獲取資料
- 基于具有正確資料的部分(我有多個部分)以始終與資料源保持最新的方式填充標題。
這是我的視圖模型:
class ConfigViewModel{
private let disposeBag = DisposeBag()
let sections:BehaviorSubject<[ConfigStatusSectionModel]> = BehaviorSubject(value: [])
func startObserving(){
let observable = getDefaults()
observable.map { conditions -> [ConfigStatusSectionModel] in
return self.createDatasource(with: conditions)
}.bind(to: self.sections).disposed(by: disposeBag)
}
private func getDefaults()->Observable<ConfigDefaultConditionsModel> {
return Observable.create { observer in
FirebaseManager.shared.getConfigDefaults { conditions in
observer.onNext(conditions!)
} failure: { error in
observer.onError(error!)
}
return Disposables.create()
}
}
private func createDatasource(with defaults:ConfigDefaultConditionsModel)->[ConfigStatusSectionModel]{
let firstSectionItems = defaults.start.elements.map{ConfigItemModel(item: $0, data: nil)}
let firstSection = ConfigStatusSectionModel(model: defaults.start.title, items: firstSectionItems.compactMap{ConfigCellModel(model: $0)})
let secondSectionItems = defaults.stop.elements.map{ConfigItemModel(item: $0, data: nil)}
let secondSection = ConfigStatusSectionModel(model: defaults.stop.title, items: secondSectionItems.compactMap{ConfigCellModel(model: $0)})
let sections:[ConfigStatusSectionModel] = [firstSection, secondSection]
return sections
}
}
現在我能做的是設定一個 tableview 委托,如下所示:
tableView.rx.setDelegate(self).disposed(by: disposeBag)
然后實作適當的委托方法來創建/回傳自定義標頭:
extension BotConfigViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,
viewForHeaderInSection section: Int) -> UIView? {
guard let header = tableView.dequeueReusableHeaderFooterView(
withIdentifier: ConfigSectionTableViewHeader.identifier)
as? ConfigSectionTableViewHeader
else {
return nil
}
return header
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}
如何使用來自我的資料源的資料填充我的自定義標頭?我不想做類似的事情switch (section){...},因為它完全不與資料源同步,而是手動同步,如果資料源發生變化,它不會自動影響標頭配置。
這是我的模型結構:
typealias ConfigStatusSectionModel = AnimatableSectionModel<String, ConfigCellModel>
struct ConfigItemData {
let conditionsLink:String?
let iconPath:String?
}
struct ConfigItemModel {
let item:OrderConditionModel
let data:ConfigItemData?
}
struct ConfigCellModel : Equatable, IdentifiableType {
static func == (lhs: ConfigCellModel, rhs: ConfigCellModel) -> Bool {
return lhs.model.item.symbol == rhs.model.item.symbol
}
var identity: String {
return model.item.symbol
}
let model: ConfigItemModel
}
我嘗試使用它,但無法使其完全作業,因為我想我沒有以正確的方式/時刻提供自定義標題。
uj5u.com熱心網友回復:
這里的基本問題是這tableView(_:viewForHeaderInSection:)是一種基于拉的方法,而 Rx 是為基于推的系統設計的。顯然是可以做到的。畢竟,基礎庫是為此而做tableView(_:cellForRowAt:)的,但它要復雜得多。您可以遵循基本庫用于后一個功能的相同系統。
下面就是這樣一個系統。它可以這樣使用:
source
.bind(to: tableView.rx.viewForHeaderInSection(
identifier: ConfigSectionTableViewHeader.identifier,
viewType: ConfigSectionTableViewHeader.self
)) { section, element, view in
view.setup(data: element.model)
}
.disposed(by: disposeBag)
這是使上述成為可能的代碼:
extension Reactive where Base: UITableView {
func viewForHeaderInSection<Sequence: Swift.Sequence, View: UITableViewHeaderFooterView, Source: ObservableType>
(identifier: String, viewType: View.Type = View.self)
-> (_ source: Source)
-> (_ configure: @escaping (Int, Sequence.Element, View) -> Void)
-> Disposable
where Source.Element == Sequence {
{ source in
{ builder in
let delegate = RxTableViewDelegate<Sequence, View>(identifier: identifier, builder: builder)
base.rx.delegate.setForwardToDelegate(delegate, retainDelegate: false)
return source
.concat(Observable.never())
.subscribe(onNext: { [weak base] elements in
delegate.pushElements(elements)
base?.reloadData()
})
}
}
}
}
final class RxTableViewDelegate<Sequence, View: UITableViewHeaderFooterView>: NSObject, UITableViewDelegate where Sequence: Swift.Sequence {
let build: (Int, Sequence.Element, View) -> Void
let identifier: String
private var elements: [Sequence.Element] = []
init(identifier: String, builder: @escaping (Int, Sequence.Element, View) -> Void) {
self.identifier = identifier
self.build = builder
}
func pushElements(_ elements: Sequence) {
self.elements = Array(elements)
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: identifier) as? View else { return nil }
build(section, elements[section], view)
return view
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/429704.html
