Skip to content

Instantly share code, notes, and snippets.

@radianttap
Created February 19, 2018 20:30
Show Gist options
  • Save radianttap/c06445d8cd24ed636a4f12fe5370a0c5 to your computer and use it in GitHub Desktop.
Save radianttap/c06445d8cd24ed636a4f12fe5370a0c5 to your computer and use it in GitHub Desktop.
Why the hell this crashes in UICollectionView?
let expandedSections = [0, 3, 8]
let indexSet = IndexSet(expandedSections)
collectionView?.performBatchUpdates(
{
[unowned self] in
if isInserted {
self.collectionView?.insertItems(at: indexPaths)
} else {
self.collectionView?.deleteItems(at: indexPaths)
}
if !indexSet.isEmpty {
self.collectionView?.reloadSections(indexSet)
}
}, completion: {
_ in
}
@radianttap
Copy link
Author

2 years later, I have the answer. Happens when you do this:

let indexPath = IndexPath(item: NSNotFound, section: section)
let attr = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, with: indexPath)

and somewhere along the way you do collectionView.reloadSections()

Problem is indexPath, which should be:

let indexPath = IndexPath(item: 0, section: section)

@ivanglushko
Copy link

ivanglushko commented Jul 7, 2021

Hi I'm facing the same issue using framework for CollectionView well that is weird.
Thanks for posting the reason why it happens.
Although i did not understand fully why this is happening.
My only hint is that somehow indexSet is over-floating.

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