我想将可扩展的tableView从默认值传递到Rx,但是我发现无法正确使用 numberOfRownInSection 的问题。
现在的逻辑是...当结构具有标志 isExpandable = false 时,行数为 0
我所拥有的是带有静态数据的 tableView (结构将在下面提供)。 HeaderView作为标题,单元格作为可扩展内容。
切换功能:
func toggleCell(_ section: Int) {
var indexPaths = [IndexPath]()
for row in data[section].items.indices {
let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath)
}
let isExpanded = data[section].isExpanded
data[section].isExpanded = !isExpanded
if isExpanded {
categoriesTableView.deleteRows(at: indexPaths, with: .none)
} else {
categoriesTableView.insertRows(at: indexPaths, with: .none)
}
}
默认tableView委托:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: HomeViewCell = tableView.dequeueReusableCell(forIndexPath: indexPath)
cell.categoryLabel.text = data[indexPath.section].items[indexPath.row].title ?? ""
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !data[section].isExpanded {
return 0
}
return data[section].items.count
}
结构
var data: [CategoriesSectionData] = [CategoriesSectionData(header: "Elektro", items: [CategoriesCellData(price: 12.99, title: "Gärtner 1"),
CategoriesCellData(price: 15.30, title: "Gärtner 2"),
CategoriesCellData(price: 25.99, title: "Gärtner 3")], isExpanded: false, icon: "home_menu_1"),
CategoriesSectionData(header: "Gartenpflege", items: [CategoriesCellData(price: 14.0, title: "Gärtner 1")], isExpanded: false, icon: "home_menu_2"),
CategoriesSectionData(header: "Sanitär", items: [], isExpanded: false, icon: "home_menu_3"),
CategoriesSectionData(header: "Hausmeisterdienste", items: [], isExpanded: false, icon: "home_menu_4"),
CategoriesSectionData(header: "Meisterprüfung", items: [], isExpanded: false, icon: "home_menu_3"),
CategoriesSectionData(header: "Gartenpflege", items: [], isExpanded: false, icon: "home_menu_2"),
CategoriesSectionData(header: "Sanitär", items: [], isExpanded: false, icon: "home_menu_1"),
CategoriesSectionData(header: "Hausmeisterdienste", items: [], isExpanded: false, icon: "home_menu_4")]
要将其传递给Rx,我使用了以下代码,但是在插入/删除行时出现错误,因为我不想清除自己的dataSource并且无法设置 numberOfRownInSection 不显示带有可扩展标志的单元格。
let dataSource = RxTableViewSectionedReloadDataSource<CategoriesSectionData>(
configureCell: { ds, tv, indexPath, item in
let cell: HomeViewCell = tv.dequeueReusableCell(forIndexPath: indexPath)
cell.categoryLabel.text = item.title ?? ""
return cell
},
titleForHeaderInSection: { ds, index in
return ds.sectionModels[index].header
}
)
self.dataSource = dataSource
Observable.just(data)
.bind(to: categoriesTableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
categoriesTableView.rx
.setDelegate(self)
.disposed(by: disposeBag)
答案 0 :(得分:0)
尝试在GitHub上查看RxDataSources存储库。有一个有关如何执行此操作的示例。您可以通过Rx方式将cellViewModels绑定到tableview