RxSwift和UICollectionView标头
问题描述:
我使用的是RxSwift
,我的UIViewController
包含UICollectionView
。我已经在努力将头添加到我的收藏看法,但这个不会被调用:RxSwift和UICollectionView标头
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath)
答
它无关,跟RxSwift
除非你使用RxDataSources
,那是。
你只是没有正确设置你的UICollectionView
。其中包括:
- 确保您定义了
func numberOfSections(in collectionView: UICollectionView) -> Int
以返回大于0
的内容。 - 确保您定义
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
返回的尺寸大于CGSize(width: 0, height: 0)
。 - 如果您使用的是
nib
,请将其注册为func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
。或者,如果您以编程方式创建它,请将其注册为func register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
。
答
我有同样的问题。解决它移动到RxCollectionViewSectionedReloadDataSource
这种类型的数据源有页眉/页脚。标准RxCollectionViewReactiveArrayDataSource没有页眉/页脚。
即使在RxCocoa UICollectionView+Rx扩展中,也以RxCollectionViewSectionedReloadDataSource为例。
例子:
let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, Double>>()
let items = Observable.just([
SectionModel(model: "First section", items: [
1.0,
2.0,
3.0
]),
SectionModel(model: "Second section", items: [
1.0,
2.0,
3.0
]),
SectionModel(model: "Third section", items: [
1.0,
2.0,
3.0
])
])
dataSource.configureCell = { (dataSource, cv, indexPath, element) in
let cell = cv.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! NumberCell
cell.value?.text = "\(element) @ row \(indexPath.row)"
return cell
}
items
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
+0
欢迎来到SO。请格式化您的答案,这里是您需要做的信息的链接https://meta.stackexchange.com/help/formatting –
我已经试过您的解决方案,它并没有帮助。我的假设是,在UICollectionViewDataSource协议中定义了func collectionView(collectionView:UICollectionView,viewForSupplementaryElementOfKind kind:String,atIndexPath indexPath:NSIndexPath),它由RxSwift实现。它可能是什么?也许我应该用更多的信息更新我的问题? – alexxjk
我不认为添加更多信息会有帮助,因为您基本上必须交出所有代码。我建议查看一下'UICollectionView'教程,并严格按照它来确保你获得了一些基础工作。然后从那里修改它。 – solidcell