Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using a collectionView with a custom UICollectionViewLayout. #241

Open
jdisho opened this issue Jun 22, 2018 · 17 comments
Open

Comments

@jdisho
Copy link
Member

jdisho commented Jun 22, 2018

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. 😊

@Vkt0r
Copy link
Member

Vkt0r commented Jun 25, 2018

@jdisho It would be helpful to see an example of your project 🤔. Maybe It can be an issue in the library, but I have custom layouts implemented in some UICollectionViews and I don't have any issue with the library.

@freak4pc
Copy link
Member

Hey @jdisho - Did you work this out? If not, as @Vkt0r mentioned, a project would be very helpful.

@jdisho
Copy link
Member Author

jdisho commented Aug 24, 2018

Hey @freak4pc - Thanks for reminding me.

@jdisho
Copy link
Member Author

jdisho commented Aug 28, 2018

Have a look at Papr @freak4pc

SearchPhoto is responsible group.
To replicate the crash, go to Search -> search something -> Photos with "..."

@mkko
Copy link

mkko commented Aug 28, 2018

What environment are you using? I tried but couldn't reproduce the crash with Xcode Version 9.4.1 (9F2000) and iPhone X Simulator with 11.4, on High Sierra.

@freak4pc
Copy link
Member

Can't repro as well on Mojave Xcode 9.4.1

@jdisho
Copy link
Member Author

jdisho commented Aug 28, 2018

Are you in rxdatasources-not-working branch?

@freak4pc
Copy link
Member

You didn't mention a branch ;-)

@jdisho
Copy link
Member Author

jdisho commented Aug 28, 2018

Sorry for the confusion. However, the link is with that branch 😄

@mkko
Copy link

mkko commented Aug 28, 2018

The link directed to that branch. For some reason I still managed to download the -dev branch version.

@mkko
Copy link

mkko commented Aug 28, 2018

Is this it?

        for item in 0..<collectionView.numberOfItems(inSection: 0) {

Line 54 in your new layout, PinterestLayout.

@jdisho
Copy link
Member Author

jdisho commented Aug 28, 2018

Yes it is, but I don't expect 0 sections. Probably I am missing something from RxDataSources

@mkko
Copy link

mkko commented Aug 28, 2018

It looked like the whole observable sequence didn't even start yet, so this seems to happen before the items are in the collection view. If you add .debug() to the sequence where you do the binding, there's no output before it crashes because of the layout.

@jdisho
Copy link
Member Author

jdisho commented Aug 28, 2018

You are right, but how is the normal flow layout is handling this then?! 🤔

@bordoli
Copy link

bordoli commented Dec 14, 2018

In your prepare() implementation add a check of numberOfSections

guard cache.isEmpty == true,
    let collectionView = collectionView,
    collectionView.numberOfSections > 0 else { return }

😀

@User2004
Copy link

Click here for set custom collection view flow layout i objective C

Click here for set custom collection cell in collectionview in swift

@168-7cm
Copy link

168-7cm commented Dec 5, 2022

@bordoli
sorry for mentioning you🙏
why we should check numberOfSections > 0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants