Note that there are some explanatory texts on larger screens.

plurals
  1. POUICollectionView not recognized in collectionView numberOfItemsInSection:
    primarykey
    data
    text
    <p>I have a problem I can't solve for some hours now...</p> <p>I have multiple UICollectionViews with different numbers of cells and different cellsizes. The CollectionViews are created programmatically and the delegates and datasources are set.</p> <p>The collectionviews are created like this:</p> <pre><code>UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; collectionViewOne = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,320,150) collectionViewLayout:layout]; [collectionViewOne setTag:99]; [collectionViewOne setDataSource:self]; [collectionViewOne setDelegate:self]; [collectionViewOne registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; [collectionViewOne setBackgroundColor:bgColor]; [collectionViewOne setShowsHorizontalScrollIndicator:NO]; [collectionViewOne setBounces:YES]; [collectionViewOne setAlwaysBounceHorizontal:YES]; [collectionViewOne setScrollEnabled:YES]; [collectionViewOne setRestorationIdentifier:@"collectionViewOne"]; [scrollView addSubview:collectionViewOne]; </code></pre> <p>My functions are like these:</p> <pre><code>- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section { NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); if (collectionView == collectionViewOne) { return 3; } else if (collectionView == collectionViewTwo) { return 4; } else { return 1; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); if (collectionView == collectionViewOne) { return CGSizeMake(100, 150); } else if (collectionView == collectionViewTwo) { return CGSizeMake(200, 150); } else { return CGSizeMake(200, 150); } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"restorationIdentifier: %@", collectionView.restorationIdentifier); if (collectionView == collectionViewOne) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; imageView.image = [UIImage imageNamed:@"image.png"]; [cell addSubview:imageView]; } else if (collectionView == collectionViewTwo) { //create cell of type 2 } return cell; } </code></pre> <p>In my log I get the following output (for example):</p> <p><strong>restorationIdentifier in numberOfItemsInSection: (null) restorationIdentifier in sizeForItemAtIndexPath : (null) restorationIdentifier cellForItemAtIndexPath: collectionViewOne</strong></p> <p>collectionViewOne is the restorationIdentifier on collectionViewOne. So why is it not recognized in the first two methods?</p> <p>Ofcourse the result is that I get crashes because the cellsizes and number of cells in the different CollectionViews are not properly set. The error:</p> <pre><code>*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-2903.2/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: &lt;NSIndexPath: 0x8c31c10&gt; {length = 2, path = 0 - 2}' </code></pre> <p>How can I fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload