一. 只有一个分区的简单UITableView
使用 tableView.rx.items(cellIdentifier: "xxx", cellType: xxx))
1: 注册cell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIndentifier")
2: 创建 DataSource, 将 DataSource 绑定到cell
let dataSource = Observable<[String]>.just(["first element", "second element", "third element"])
dataSource.bind(to: tableView.rx.items(cellIdentifier: "reuseIdentifier", cellType: UITableViewCell.self)) { index, model, cell in
cell.textLabel?.text = model
}
.disposed(by: disposeBag)
3: cell 点击事件
Observable.zip(tableView.rx.itemSelected, tableView.rx.modelSelected(String.self)).bind { [weak self] (index, item) in
guard let weakSelf = self else { return }
}.disposed(by: bag)
tableView.rx.modelSelected(Model.self).bind { [weak self] (model) in
guard let weakSelf = self else { return }
}.disposed(by: disposeBag)
有 Header 的 tableView
iOS RxDataSource 构建有header 的 UITableView_Leecsdn77的博客-CSDN博客
二. 多个分区的 UITableView
1: 注册cell
2. 使用SectionModel创建cell
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String,Model>>(configureCell: { [weak self] (dataSource, tableView, index, item) -> UITableViewCell in
guard (self != nil) else { return UITableViewCell(style: .default, reuseIdentifier: nil) }
let cell = tb.dequeueReusableCell(withIdentifier: "xxx", for: index)
return cell
})
3. 将数据源绑定的 cell
dataSourceObs.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
4: section header, footer
dataSource.configureSupplementaryView = { [weak self] (dataSource, collect, kind, index) -> UICollectionReusableView in
guard let self = self else { return UICollectionViewCell() }
if kind == UICollectionView.elementKindSectionHeader {
let headerView = collect.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: self.headerCellID, for: idx) as! CityHeaderCollectionViewCell
headerView.titleLabel.text = (ds.sectionModels[idx.section]).model
headerView.titleLabel.setLabelColorAndFont(color: ColorLightBlack, size: 14)
return headerView
}
return UICollectionViewCell()
}
或者同时创建 cell, header, footer
lazy var dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, Model>>.init(configureCell: { [weak self]dataSource, collectionView, indexPath, item in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? Cell else {
return UICollectionViewCell()
}
cell.config(with: item, didTapped: {
self?.updateUI(with: indexPath)
})
return cell
}, configureSupplementaryView: { [weak self] dataSource, collectionView, section, indexPath in
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SectionHeader", for: indexPath) as? SectionHeader else {
return UICollectionReusableView()
}
header.config(with: dataSource[indexPath.section].model)
return header
})
另一种写法:
struct CustomData {
var anInt: Int
var aString: String
var aCGPoint: CGPoint
}
struct SectionOfCustomData {
var header: String
var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
typealias Item = CustomData
init(original: SectionOfCustomData, items: [Item]) {
self = original
self.items = items
}
}
let dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>(
configureCell: { dataSource, tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "Item \(item.anInt): \(item.aString) - \(item.aCGPoint.x):\(item.aCGPoint.y)"
return cell
})