https://github.com/ReactiveX/RxSwift/tree/master/RxExample/RxDataSources
RxDataSources:
https://github.com/RxSwiftCommunity/RxDataSources
Podfileに記載するなりしてインストールしてください。
SectionありのdataSourceを生成する
RxTableViewSectionedReloadDataSourceを使用します。SectionModelでSectionに表示するelementを指定します。
let dataSource = RxTableViewSectionedReloadDataSource<SectionModel<String, String>>()
let items = Observable.just([
SectionModel(model: "First section", items: [
"aaaa",
"bbbb",
]),
SectionModel(model: "Second section", items: [
"cccc",
"dddd",
])
])
セルの生成
以下のように、indexPathとelementが引数に入っているのでコールバック内でセルを生成する。
dataSource.configureCell = { (_, tableView, indexPath, element) in
let cell = tv.dequeueReusableCellWithIdentifier("Cell")!
cell.textLabel?.text = "\(element) @ row \(indexPath.row)"
return cell
}
データとTablewViewをBindする
items
.bindTo(tableView.rx_itemsWithDataSource(dataSource))
.addDisposableTo(disposeBag)
ヘッダーのカスタマイズ
UITableViewDelegateで処理する必要があります。以下のように、ヘッダー用のViewとHightを返すモジュールを実装します。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UITableViewHeaderFooterView()
view.textLabel?.text = dataSource.sectionAtIndex(section).model ?? ""
return view
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
0 件のコメント:
コメントを投稿