RxDataSources: Error when using a collectionView with a custom UICollectionViewLayout.
Hi 👋
I have created a custom UICollectionViewLayout.
protocol PinterestLayoutDelegate: class {
func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -> CGFloat
}
class PinterestLayout: UICollectionViewLayout {
// MARK: Delegate
weak var delegate: PinterestLayoutDelegate!
// MARK: Overrides
override var collectionViewContentSize: CGSize {
...
}
override func prepare() {
...
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
...
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
...
}
}
Then in a ViewController I am configuring a collectionView with the PinterestLayout and also the dataSource, as following:
override func viewDidLoad() {
super.viewDidLoad()
viewModel.searchPhotosCellModel
.map { [SearchPhotosSectionModel(model: "", items: $0)] }
.bind(to: collectionView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
...
private func configureCollectionView() {
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: PinterestLayout())
view.addSubview(collectionView)
collectionView.registerCell(type: SearchPhotosCell.self)
dataSource = RxCollectionViewSectionedReloadDataSource<SearchPhotosSectionModel>(
configureCell: collectionViewDataSource
)
}
private var collectionViewDataSource: CollectionViewSectionedDataSource<SearchPhotosSectionModel>.ConfigureCell {
return { _, collectionView, indexPath, cellModel in
var cell = collectionView.dequeueReusableCell(
type: SearchPhotosCell.self,
forIndexPath: indexPath)
cell.bind(to: cellModel)
return cell
}
}
The problem that I am occurring when I am running the app is that I get the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for number of items in section 0 when there are only 0 sections in the collection view
I think this issue is related to RxDataSource underlying implementation. However, if you think it is not, it would be very helpful to show an example how to use this library with custom UICollectionViewLayout-s. 😊
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 4
- Comments: 17 (4 by maintainers)
In your prepare() implementation add a check of numberOfSections
😀
Is this it?
Line 54 in your new layout,
PinterestLayout.Hey @freak4pc - Thanks for reminding me.