我正在嘗試使用 RxTableViewSectionedAnimatedDataSource 實作 tableView,我將所有子類設定正確,當我嘗試將 dataSource 系結到我的 tableView 時,編譯器不斷警告我
實體方法 'items(dataSource:)' 要求 'TableViewSectionedDataSource' 符合 'RxTableViewDataSourceType'

這是代碼
let tableView = UITableView()
let dataSource = RxTableViewSectionedAnimatedDataSource<CustomSectionDataType>(configureCell: { dataSource, tableView, indexPath, item in
return UITableViewCell()
})
dataSource.titleForHeaderInSection = { (ds, section) in
let sectionModel = ds.sectionModels[section]
return sectionModel.header
}
let sectionDatas = [CustomSectionDataType(ID: "1", header: "test", items: ["WTF!"])]
let items = BehaviorRelay(value: [sectionDatas])
items
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: self.disposeBag)
自定義部分類
struct CustomSectionDataType {
var ID: String
var header: String
var items: [Item]
}
extension CustomSectionDataType: AnimatableSectionModelType {
typealias Item = String
typealias Identity = String
var identity: String {
return ID
}
init(original: CustomSectionDataType, items: [Item]) {
self = original
self.items = items
}
}
uj5u.com熱心網友回復:
您的items型別嵌套太深的陣列。
型別被定義為何BehaviorRelay<[[CustomSectionDataType]]>時應該是BehaviorRelay<[CustomSectionDataType]>。
另外,請考慮使用 typealias 而不是創建自己的自定義型別:
typealias CustomSectionDataType = AnimatableSectionModel<String, String>
或者,如果您有兩個相同但 ID 不同的標頭,則:
struct CustomModel: IdentifiableType {
var identity: String
var header: String
}
typealias CustomSectionDataType = AnimatableSectionModel<CustomModel, String>
它讓生活更輕松一些。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/363806.html
