我想将可扩展的numberOfRownInSection.从默认的tableView传递给Rx,但是我发现无法正确使用Rx的问题
现在逻辑是..。当结构有标志isExpandable = false时,行计数为
我所拥有的是带有静态数据的tableView (struct将在下面提供)。HeaderView作为标题,单元作为可扩展的内容。
Toggle func:
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,我使用了下面的代码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)发布于 2019-12-18 13:54:06
尝试查看RxDataSources回购在GitHub上。关于如何做到这一点,有一个例子。您可以以Rx方式将cellViewModels绑定到tableview。
https://stackoverflow.com/questions/58937977
复制相似问题