iOS RxDatasource 使用

一. 只有一个分区的简单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
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值