iOS 15 CollectionView Crash (Xcode 13)

On iOS 15 builds created with Xcode 13, a collectionView is crashing when

collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .top)

is called and it has no items in its datasource yet.

Crash Message:

Attempted to scroll the collection view to an out-of-bounds section (0) when there are only 0 sections.

Note

This does not happen on any builds created by versions of Xcode before Xcode 13.

Is this an expected change or something that needs to be addressed?

  • I have the same issue here, has anyone found a solution that works on Xcode 13.2.1?

  • Same issue in iOS 15.2 build with Xcode 13.2.1. Not an issue build with same Xcode version for 14.*

Add a Comment

Replies

On iOS 15 builds created with Xcode 13, a collectionView is crashing when is called and it has no items in its datasource yet.

That seems pretty logical. Why is dataSource still empty leading to section count of 0 ?

That may be a new detection done with Xcode 13, to correct an undetected error in Xcode 12 (may be by default, even if you did not define, number of sections was defaulted to 1. That may not be the case anymore.

Could you show the code involving the dataSource ?

  • Moving to an answer... sorry

Add a Comment

You are totally correct. With the snipped below it was totally possible for the number of sections to be 0.

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return answerViewModels.count
}

However just checking in to see why, if it actually exists, any changes to a detection was not made aware in the release notes.

Release notes are just that: some notes, not an exhaustive list of what has changed.

Does it work now ? If so, don't forget to close the thread on the correct answer.

Not sure I'm following you here? Release Notes on a major version of Xcode are a little bit more than just mere notes.

In addition, the crash still occurs when numberOfSections is 1 and the numberOfItemsInSection is 0. That was definitely not the case in pervious versions of Xcode.

  • This - it's happening to me too in iOS 15, my app works fine in XCode 14 built apps - suggest filing a radar - it's almost certainly a bug.

  • Just clarifying, Do you mean Xcode 12 built apps?

    Thanks for letting me know I'm not the one this is happening to. Definitely going to file a radar since it totally seems like this is a bug.

  • Duh...! Sorry, yeah Xcode 12 - unfortunately I frequently flip the OS and Xcode versions talking with colleagues :)

Add a Comment

I have the same problem

I find the reason of the problem Problem is in Scroll to some index in empty tableView Check your tableview data before scrolling

I'm with the same problem, can you tell me if it's solved?

same issue

Not fixed in yesterday's Xcode 13.1 release

  • same issue, anyone find solution?

  • A workaround for now that I used was to just guard against the data that is driving the CollectionView from being empty. Still no word on what has actually changed in Xcode

Add a Comment

I am seeing the same issue on Xcode 13.2 beta. After running the code under Xcode 12.5.1, This log message appears:

 [Assert] Attempted to scroll the collection view to an out-of-bounds item (0) when there are only 0 items in section 0.
 This will become an assert in a future version.

I guess this is the future version when it was decided to assert.

You could try to guard the code from being executed:

guard indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) else {return}

I have the same issue here, has anyone found a solution that works on Xcode 13.2.1?

Same issue here, crash reported in Crashlytics in the following statement:

collectionView.scrollToItem(at: index, at: .centeredHorizontally, animated: false)

Fatal Exception: NSInternalInconsistencyException  Attempted to scroll the collection view to an out-of-bounds item (25) when there are only 25 items in section 0. 

Note: No issue reported before version Xcode 13.

FYI - numberOfSections is hardcoded as 1