8

I "https://www.raywenderlich.com/392-uicollectionview-custom-layout-tutorial-pinterest" To create a Custom UICollectionView. (To adjust cell height)

If I scroll up, the cell will continue to be added, and scroll from top to bottom to refresh.

There is no problem when you run your app and initially grow Cells. However, there is always an error when refreshing or reordering the number of cells.

ERROR :

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.93.8/UICollectionViewData.m:447

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist :

Therefore, "iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist" And tried to solve the error.

on the line below

self.artCollectionView.reloadData () 

I tried adding

self.artCollectionView.collectionViewLayout.invalidateLayout () 

but it was not resolved.

Also, I do not know where to run

cache.removeAll ()

in the prepare () section. (There is a prepare () function in PinterestLayout.swift)

What is the correct way to resolve the error?

2
  • can you post the full code that you're working with. The error just means when it checks to see how many cells there are its expecting an index number thats beyond the cell count. Commented Nov 13, 2018 at 7:32
  • I've resolved it, but I'll add the source code for everyone. Commented Nov 13, 2018 at 7:48

4 Answers 4

10

There is prepare function in PinterestLayout.swift as it should be because it is the only layout used for customizing the collectionView.

At line 76, in PinterestLayout.swift file.

Try

override public func prepare() {
    cache.removeAll()
    if cache.isEmpty {
       .....
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3
extension UICollectionView{
   func refreshLayout() {
        let oldLayout = collectionViewLayout as! UICollectionViewFlowLayout
        let newLayout = UICollectionViewFlowLayout()
        newLayout.estimatedItemSize = oldLayout.estimatedItemSize
        newLayout.footerReferenceSize = oldLayout.footerReferenceSize
        newLayout.headerReferenceSize = oldLayout.headerReferenceSize
        newLayout.itemSize = oldLayout.itemSize
        newLayout.minimumInteritemSpacing = oldLayout.minimumInteritemSpacing
        newLayout.minimumLineSpacing = oldLayout.minimumLineSpacing
        newLayout.scrollDirection = oldLayout.scrollDirection
        newLayout.sectionFootersPinToVisibleBounds = oldLayout.sectionFootersPinToVisibleBounds
        newLayout.sectionHeadersPinToVisibleBounds = oldLayout.sectionHeadersPinToVisibleBounds
        newLayout.sectionInset = oldLayout.sectionInset
        newLayout.sectionInsetReference = oldLayout.sectionInsetReference
        collectionViewLayout = newLayout
    }
  }

Then call:

YourCollectionView.refreshLayout()
YourCollectionView.reloadData()

Happy Coding!

Comments

3

Received this error when I was accidentally setting a collectionView cell's height to 0. Make sure height (or any dimension) is greater than 0! Making sure it was always greater than 0 solved the issue for me.

2 Comments

This solved my problem, can anyone explain why this crash happens only on iOS 12? it works normal on iOS13 when I set an empty collection view height to zero.
@zzmasoud Hm, that's interesting behavior. Maybe it was a bug in iOS 12.. Not sure.. Maybe someone has an answer
1

In my case, because I using same layout(UICollectionViewFLowLayout) object for three collectionViews, so, change to each collectionView using different layout object(create three same new layout), this crash will gone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.