To use a non-animated data source like RxTableViewSectionedReloadDataSource, follow these steps:
- Define your section model: Create a struct that conforms to
SectionModelType. It must define a typealias Item and an items property of type [Item]. The struct must also implement an initializer init(original: SectionModelType, items: [Item]). - Create the data source: Instantiate the desired data source type (e.g.,
RxTableViewSectionedReloadDataSource) and provide a configureCell closure. - Customize closures: Optionally set properties like
titleForHeaderInSection, titleForFooterInSection, canEditRowAtIndexPath, or canMoveRowAtIndexPath. - Bind to TableView: Create an
Observable sequence of your section models and bind it to tableView.rx.items(dataSource: dataSource).
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
})
// Customization example
dataSource.titleForHeaderInSection = { dataSource, index in
return dataSource.sectionModels[index].header
}
// Binding
let sections = [
SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
SectionOfCustomData(header: "Second section", items: [CustomData(anInt: 2, aString: "two", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 3, aString: "three", aCGPoint: CGPoint(x: 3, y: 3)) ])
]
Observable.just(sections)
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)