我用RxDatasources
来创建我的数据源.稍后,我将在视图控制器中配置单元格.问题是,因为页眉/页脚与数据源无关(除了我们可以设置标题,但如果我们使用自定义页眉/页脚,这个标题将被覆盖).
下面是我如何配置tableview单元格的:
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){...}
这样的事情,因为那样它与数据源完全不同步,而是手动的,如果数据源改变,它不会自动影响头配置.
以下是我的模型 struct :
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
}
我试着使用this,但是我不能让它完全工作,因为我想我没有以正确的方式/时机提供自定义标题.