What caused this iOS error? UICollectionView received layout attributes for a cell with a pointer path that does not exist

I am working on an application that has a UICollectionViewController that crashes in certain mysterious situations that are difficult to reproduce. The failure log is as follows:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewData.m:417 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000008016> {length = 2, path = 0 - 1}' 

Such crashes seem to have just begun in our code after we switched to the iOS 8 SDK.

Why is this happening?

Note. I already know the answer to the question, but I found very little information about this crash in Qaru and the rest of the Internet. I will post an answer below. This mistake made my colleague and I hunt for three days, so I hope this post will save someone else a lot of time and frustration. I registered this error with Apple.

+30
ios objective-c uicollectionview
Dec 16 '14 at 23:13
source share
10 answers

The accident occurred in the following situation:

We had a collection view controller that represented another view controller on it.

While the collection view controller was no longer visible, the following sequence of events occurred in response to our backward queries.

  • [UICollectionView insertItemsAtIndexPaths:] was called with 50 items as a collection of hidden UICollectionViewController .
  • [UICollectionView reloadData] was invoked in a hidden collection view.
  • There was a short delay.
  • The number of elements in the hidden view of the collection was set to a small number.
  • [UICollectionView reloadData] was called again.
  • The view controller was fired by showing the hidden collection view controller.

A confirmation error in the UIKit UICollectionViewData inner class will occur in step 6.

So, in the lesson, try to avoid manipulating the collection view, which is not displayed on the screen.

Our method of solving this problem was to call [UICollectionView reloadSections:] instead of [UICollectionView reloadData] at key points.

We suspect that the effects of reloadData delayed to some point in the future, and therefore there are subtle problems with how this can interact with other method calls, such as insertItemsAtIndexPaths , while reloadSections processed immediately, leaving the collection view in better condition.

We think that we did not see this behavior until we started creating our application for iOS 8.

Sleep well friends!

+27
Dec 16 '14 at 23:26
source share

For me it was an array of UICollectionViewLayoutAttribute. I use it in a UICollectionViewLayout to store an attribute of an element.

I forgot to clear it in the prepareLayout method.

Thus, layoutAttributesForItemAtIndexPath returned incorrect values ​​for indexPath, resulting in the same crash.

Only with removeAll in the array at the beginning of prepareLayout does it work.

+20
Jan 11 '16 at 9:34
source share

I’ve been working on the same error for some time and I think I found a different source of errors on iOS 7, but it works fine on iOS 8, which causes the same exact error: Auto-Layout!

I am using a control in which UICollectionViews, a grid, are embedded. I noticed hacking the code to remove the UICollectionViewLayoutAttributes when there is a mismatch with the UICollectionView data, resulting in a crash that other controls in my view were inappropriate.

The contents of my collection are "static", loaded at boot time and does not allow for insecure changes. Again this works great with iOS 8.

So, I turned off the automatic layout function only for this view and BINGO! The crash has disappeared. Auto-layout was played with an internal UICollectionSize, so the (NSArray *) layoutAttributesForElementsInRect: (CGRect) method returned the mixing results.

I tried reloadData, reloadSections, does nothing, but it works!

Hope this helps someone else fight UIKit exception with this control.

+6
Jan 29 '15 at 18:49
source share

collectionViewLayout caches attributes. In viewwillappear - create a new instance of the ViewLayout collection and assign it to collectionview.collectionViewLayout Thus, all cached attributes will be cleared before rebooting. Your problem can be solved. Worked for me, especially when you use other collectionViewLayout libraries.

+6
Apr 09 '15 at 13:13
source share

Xcode 8 - Swift 3

In my case, this error was caused by Autolayout; I have a View collection built into the UIView that I hide by setting the height to 0 when the Collection is empty and returns to 150 when I need to show the CollectionView again.

I managed to remove the error by calling

collectionView.collectionViewLayout.invalitdateLayout ()

before I ran the code that animates the call to layoutIfNeeded () in superView. Now it's pretty smooth.

Hope this can help someone in the future.

 var sponsoredPlaceSummaries: [PlaceSummary] = [] { didSet { if sponsoredPlaceSummaries.isEmpty { self.sponsoredPlacesViewHeight.constant = 0 self.collectionView.collectionViewLayout.invalidateLayout() UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseInOut, animations: { self.view.layoutIfNeeded() }, completion: nil) } else if self.sponsoredPlacesViewHeight.constant != 150 { self.sponsoredPlacesViewHeight.constant = 150 UIView.animate(withDuration: 1.0, delay: 0, options: .curveEaseInOut, animations: { self.view.layoutIfNeeded() }, completion: nil) } } 
+4
Jul 19 '17 at 23:57
source share

it helped me:

 cell.collectionView.collectionViewLayout.invalidateLayout() cell.collectionView.reloadData() cell.collectionView.layoutSubviews() 
+3
Jul 01 '17 at 4:35 on
source share

I met the same problem. In my situation, I have 2 UICollectionViews on the screen and they are the same size.

So, I am reusing the same UICollectionViewLayout as the initial parameter <- This is the problem.

2 UICollectionView should use a different UICollectionViewLayout parameter. So, just a new UICollectionViewLayout for the second UICollectionView.

+1
May 30 '16 at 2:04
source share

For me, this was due to @Drew's answer (my collection view was turned off). I manipulated the height limit of the View collection to collapse it when there is no data. This caused this crash (sometimes!). I put collectionView in a different view and reassigned @IBOutlet to this new view height limit. And made the collection height of the View constant. Crash is gone forever!

+1
Jul 02 '17 at 15:54
source share

Make sure your data source and data collection delegates are connected to the correct controller. I once got this crash since I accidentally rolled back the changes in my Main.Storyboard and because the data source and delegates remained disconnected

0
Dec 08 '16 at 23:17
source share

I was getting the same error, but this happened in a rather random way, since I could not recreate the error sequentially. I used customLayout, where I set the attributes for the elements manually. I would do one thing on my real iPhone, which would break it, but on the xCode simulator this will work. What ultimately fixed my problem was using collectionView.reloadData () instead of collectionView.reloadSections ([mySectionNum]). I have no idea why it worked sometimes and not others. It seems that he should have collapsed all the time, but this is not so. I also don't know why this fix works.

0
Mar 11 '17 at 2:21 on
source share



All Articles