swift - Swift 5 RxDataSource 可扩展单元格
问题描述
我想将可扩展的 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)
解决方案
尝试查看 GitHub 上的 RxDataSources 存储库。有一个关于如何执行此操作的示例。您可以通过 Rx 方式将 cellViewModels 绑定到 tableview
推荐阅读
- python - Plotly Trendline for Line Chart
- python - vimeo_dl:TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是 ubuntu、mac 中的 HTTPError
- apache-spark - Spark Streaming 在 Python 中计算比率/百分比
- javascript - 不能在回调中调用 React Hook “useCourtClock”
- flutter - Flutter Null 安全迁移 - StreamBuilder 快照 - 无法通过索引访问长度或数据
- python - 从 Rust 问题中使用 python:dyld:惰性符号绑定失败:未找到符号:_Py_IsInitialized
- react-native - React Native 平滑进度条填满时间
- excel - 打开时如何只更改一个工作簿?
- python - Python Gevent - 猴子 patch_all - 它适用于已编译的 .so 模块吗?
- python - 如何估计通过套接字的消息的理论 RTT?