经过很长时间弄清楚我有一个完整的答案。关键部分取决于 RxDataSources dataSource.configureCell 和 dataSource.supplementaryViewFactory。
您需要通过正常注册您的笔尖进行设置。要使集合视图显示它的标题,您需要定义标题大小。使用集合流布局允许您保留您可能在单元格内为点击处理程序提供的任何绑定,并且不会覆盖 RxCocoa 内部使用的 rx 委托代理。
在这个例子中,我有一个用于整个屏幕的 viewModel,它包含一个 RxVariable,其中包含集合视图中每个项目的 itemViewModels 数组。
带有使用 RxSwift、RxDataSources 和 RxCocoa 的标头的 UICollectionView:
var disposeBag: DisposeBag? = DisposeBag()
@IBOutlet weak var collectionView: UICollectionView!
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, ItemViewModel>>()
override func viewDidLoad() {
super.viewDidLoad()
registerHeaderCell()
registerCollectionViewCell()
setupLayout()
bindViewModelToCollectionView()
}
private func registerHeaderCell() {
let nib = UINib(nibName: "HeaderCell", bundle: nil)
collectionView.register(nib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HeaderReuseId")
}
private func registerCollectionViewCell() {
let nib = UINib(nibName:"CollectionViewCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "CellReuseId")
}
private func setupLayout(){
let layout = UICollectionViewFlowLayout()
layout.headerReferenceSize = CGSize(width: UIScreen.main.bounds.width, height: 100)
collectionView.setCollectionViewLayout(layout, animated: false)
}
private func bindViewModelToCollectionView(){
dataSource.supplementaryViewFactory = {(dataSource, collectionView, kind, indexPath) -> UICollectionReusableView in
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderReuseId", for: indexPath) as! HeaderCell
header.setup()
return header
}
dataSource.configureCell = {(datasource, collectionView, indexPath, itemViewModel) -> UICollectionViewCell in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellReuseId", for: indexPath) as! CollectionViewCell
cell.setup()
return cell
}
viewModel.itemViewModels.asObservable().map {
(itemViewModels) -> [SectionModel<String, ItemViewModel>] in
//my particular app has one big section but you can use this for mutiple sections like Sergiy's answer
return [SectionModel(model: "", items: itemViewModels)]
}.bind(to: collectionView.rx.items(dataSource: dataSource))
.addDisposableTo(disposeBag!)
}
deinit {
disposeBag = nil
}