UICollectionView approval error for stale data

In the process of trying to unload one batch of images from my view of the collection, and then replace them with another batch, I ran into an error, where, depending on whether the original or subsequent group of images was more or less planned, an assertion error occurs that states:

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

In this case, the existing list of images was 5, and the new list of samples was 2. Thus, when it hit the third image - an exception occurred - this indicates that the CollectionViewDataDelegate user interface was not aware of the change to the data stream.

Any suggestions on how to make sure the new images will reference the UICollectionView? Of course I called reloadData ...

thank

+59
ios7 uicollectionview
Aug 20 '13 at 15:26
source share
21 answers

I am running the same problem. The code runs at number 6.1 and drops to 7.0. I solved the problem as follows:

In function

 -(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView 

I'm calling

 [myCollectionView.collectionViewLayout invalidateLayout]; 

That's all.

+84
Oct. 15 '13 at 10:16
source share

Both Dominic Sander and user1544494 are correct, and their solutions are good.

Unfortunately, I noticed that if you set minimumLineSpacingForSectionAtIndex or minimumInteritemSpacingForSectionAtIndex , the look of your Collection will be broken (sooner or later).

Putting invalidateLayout in viewWillLayoutSubviews answers this question and helps maintain the look of the viewCollection.

 - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [viewCollection.collectionViewLayout invalidateLayout]; } 
+21
Jan 21 '14 at
source share

With iOS 10 and 11, this helps:

 collectionView.reloadData() collectionView.collectionViewLayout.invalidateLayout() 

Invalid layout should be AFTER data reload.

+21
Dec 27 '16 at 14:22
source share

It's simple. Just like a sentence.

 'UICollectionView recieved layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xb141c60> {length = 2, path = 0 - 2} 

This means that there is no indexPath (0,2) on dataSouce. But your UICollectionViewLayout returns a UICollectionViewLayoutAttributes for indexPath (0,2).

You must return a UICollectionViewLayoutAttributes, only exists in dataSouce only.




I think it has been changed from iOS7.

+12
Sep 25 '13 at 9:12
source share

My problem was that I had two UICollectionViews inside the same UIViewController . And I had both UICollectionViews associated with the same subclass of UICollectionViewLayout . I fixed this problem by changing each UICollectionView to have its own subclass of UICollectionViewLayout .

Source: This Question

+10
Aug 25 '14 at 10:30
source share

I fixed this crash by updating the original collection data source:

 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { [collectionView.collectionViewLayout invalidateLayout]; return collectionArray.count; } 
+4
Jan 14 '14 at 12:24
source share

I had the same collapse.

In my application, the problem was that I did not clear the array using UICollectionViewLayoutAttributes. I use it in the prepareLayout () method to store the layout attribute for each cell.

var itemAttributes: Array<UICollectionViewLayoutAttributes> = Array<UICollectionViewLayoutAttributes>()

Only with self.itemAttributes.removeAll() in the first line of prepareLayout does it work.

+2
Jan 11 '16 at 9:26
source share

I had this problem after changing the contents of the collection. The solution that worked in my case was the layout was invalid after a reboot. Doing this before rebooting will not work.

 [collectionView reloadData]; //forces the layout attributes to be recalculated for new data [collectionView.collectionViewLayout invalidateLayout]; 
+2
Dec 16 '16 at 20:51
source share

The solution I found was to ensure that the indexPath that I created in the layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? valid for string. I used to use (where i my loop counter):

 var indexPath = NSIndexPath(index: i) var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) 

But updating it to use the following, he decided:

 var indexPath = NSIndexPath(forRow: i, inSection: 0)! var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath) 
+1
Mar 17 '15 at 20:29
source share

I managed to solve this problem by subclassing UICollectionViewFlowLayout and overriding this method to return YES :

 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } 
+1
Dec 07 '15 at 17:15
source share

Make sure you update the contentSize your collectionViewLayout . Therefore, after receiving new images (2 instead of 5), recount the contentSize and set it.

0
Nov 06 '13 at 1:46
source share

I ran into this problem and it was rather unpleasant. My fix is ​​to forget about the UICollectionViewController and use the regular UIViewController with the UICollectionView inside UICollectionView .

0
Dec 02 '13 at 22:34
source share

I also had this error, and I found a workaround. For me, UICollectionView ran this under iOS 7, working fine on iOS 8.

Check out this article: What causes this iOS crash? UICollectionView received layout attributes for a cell with no pointer path

In 2 words: Auto-layout. Disable it in the view containing the UICollectionView and it worked for me.

0
Jan 29 '15 at 19:05
source share

CollectionViewLayout caches layout attributes. Quite possible. Create a new instance of collectionViewLayout 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.

0
Apr 09 '15 at 13:16
source share

I had a similar problem (using Swift2.0, Xcode 7).

Application crashed using UICollectionView received layout attributes for a cell with an index path that does not exist ...

In my case, since I used the storyboard, it turned out that I forgot to connect the IBOutlet, which was defined in my viewController, with the actual collectionView defined in the storyboard. The connection of the two is fixed.

0
Feb 27 '16 at 18:59
source share

I get it. If you use nib / xib to organize UITableViewCell and nested UICollectionView , you can avoid this error by overriding this method.

 - (void)prepareForReuse { [super prepareForReuse]; [self.collectionView.collectionViewLayout invalidateLayout]; } 

Hope this helps.

0
Feb 29 '16 at 6:55
source share

This means that there is no indexPath (0,2) on dataSouce. But your UICollectionViewLayout returns a UICollectionViewLayoutAttributes for indexPath (0,2). By TopChul

It is right! For me, the problem arose because I use the same collection layout (instance) for two collectionViews! So the layout gets confused between the two View collections.

It works fine after I use a different layout between different collection views.

0
May 12 '16 at 3:46
source share

I came across something similar when trying to duplicate a collection view in another storyboard.

'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 1 - 0}'

First I seek a quick fix. Tested copies insert various StackOverflow answers.

But I wrote my own layout class. Therefore, I try to debug unnoticed, it may be my implementation is to blame, right? It is established that the numberOfSections method numberOfSections never been called. The collection view assumes that it has only one section.

Then I discovered that the class controller class forgot to match the UICollectionViewDataSource . Although the dataSource was connected in the storyboard, it is likely that the view controller class will be disabled, for example if let ds = dataSource as? UICollectionViewDataSource {ds.numberOfSections...} if let ds = dataSource as? UICollectionViewDataSource {ds.numberOfSections...} , which will fail.

So, I added the UICollectionViewDataSource , and everything works fine. My guess may be inaccurate. But there is a lesson whenever there is a mistake with which you are not familiar, calm down and understand it. The UICollectionView got the layout attributes for a cell with a missing index path , which means exactly what it says. Is not it? Do not try to find a silver bullet, like many of the answers here. They are all great, but your code is the real battleground.

0
Dec 13 '16 at 8:20
source share

I am facing the same issue when I use UICollectionViewFlowLayout as CollectionView collectionViewLayout .

Declare the parent viewController implemented by UICollectionViewDelegateFlowLayout and assign it as collectionView , the delegate can solve this problem.

0
Jan 18 '17 at 16:22
source share

In my case, I had a custom UICollectionViewFlowLayout . After deleting cells from collectionView, the application crashed. The fix was removeAll() previously computed attributes. So, the first line after override func prepare() is arrayHoldingYourAttributes.removeAll() .

0
Jan 30 '19 at 14:49
source share

Hollo, I have the same problem while inserting the View collection into another View collection and reloadData in the main queue. Finally, I reloadData before converting the data to collectionView again.

 dispatch_async(dispatch_get_main_queue(), ^{ [_collectionView reloadData]; }); _notes = notes; [_collectionView reloadData]; 
-2
Dec 24 '15 at 5:04
source share



All Articles