Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong cells returned when dequeuing in custom UICollectionView
    primarykey
    data
    text
    <p>I'm building an UICollectionView with a custom layout.</p> <p>The layout is 3x4 with horizontal scrolling. I got the layout of the cells and the page scrolling working just fine.</p> <p>My expected result is something like this:</p> <p><img src="https://i.stack.imgur.com/MZKuk.png" alt="enter image description here"></p> <p>(available at <a href="http://www.tinyuploads.com/images/0EVQnT.png" rel="nofollow noreferrer">http://www.tinyuploads.com/images/0EVQnT.png</a>)</p> <p>However, when scrolling it seems the wrongs cells are being dequeued, and instead my actual result is this: </p> <p><img src="https://i.stack.imgur.com/jMGDR.png" alt="enter image description here"></p> <p>(available at <a href="http://www.tinyuploads.com/images/8lvJId.png" rel="nofollow noreferrer">http://www.tinyuploads.com/images/8lvJId.png</a>)</p> <p>Furthermore, when I scroll back to first page "A" is no longer in the first position.</p> <p>My datasource and delegate methods looks like this:</p> <pre><code>#pragma mark - UIViewController Life Cycle - (void)viewDidLoad { [super viewDidLoad]; self.collectionView.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f]; self.alphabet = [NSMutableArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"X", @"Y", @"Z", nil]; self.colors = [NSMutableArray arrayWithObjects:[UIColor colorWithRed:(135/255.0) green:(175/255.0) blue:(88/255.0) alpha:1], [UIColor colorWithRed:(65/255.0) green:(124/255.0) blue:(185/255.0) alpha:1], [UIColor colorWithRed:(201/255.0) green:(189/255.0) blue:(64/255.0) alpha:1], nil]; [self.collectionView reloadData]; } #pragma mark - UICollectionView datasource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.alphabet.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = [self.alphabet objectAtIndex:indexPath.row]; UIColor *backgroundColor = [self.colors objectAtIndex:indexPath.row % 3]; CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoryCellIdentifier forIndexPath:indexPath]; cell.title.text = title; cell.backgroundColor = backgroundColor; [cell setNeedsLayout]; return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"You touched: %d", indexPath.row); } </code></pre> <p>I'm in doubt of as how I should be thinking of sections. As you see here I have just one section containing all my items (contents of the alphabet).</p> <p>Any help is highly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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