2

I am hooking up a standard UICollectionView with custom cells.

Long story short, when I reload the collectionView, the cells are not dequeued in the order that they were created in.

EX. The cell for indexPath.row 5 is given back for indexPath.row 0.

This causes a flash when I reload the collectionView. I want to reuse the same cell when I reload the collectionView, but don't want to reload the data if it's the same. Because it's the wrong order, it's never the same.

Has anyone else been bothered by this or have a fix? UITableView doesn't do this, it uses the cells in the same order, why doesn't the collectionView?

- (id)initWithUser:(User *)user andCollectionView:(UICollectionView *)collectionView {
    if (self = [super initWithCollectionView:collectionView]) {
        _collectionView                                     = collectionView;

        _collectionView.dataSource                          = self;
        _collectionView.delegate                            = self;

        _collectionView.showsVerticalScrollIndicator        = YES;
        _collectionView.pagingEnabled                       = NO;
        _collectionView.bounces                             = YES;
        _collectionView.alwaysBounceVertical                = YES;
        [_collectionView registerClass:[SubmissionMinimalCollectionViewCell class] 
            forCellWithReuseIdentifier:SubmissionCollectionCellIdentifier];

        UIEdgeInsets scrollInsets                           = self.collectionView.scrollIndicatorInsets;
        scrollInsets.right                                  = 1;
        _collectionView.scrollIndicatorInsets               = scrollInsets;

        __weak typeof(self) weakSelf                        = self;
        self.data = [self.user fetchSubmissionsByPage:self.pageCount 
                                       objectsPerPage:self.perPage 
                                      andForceRefresh:YES 
                                       withCompletion:^(NSArray *submissions) {
            weakSelf.pageCount++;
            weakSelf.data                                   = submissions;
            [weakSelf.collectionView reloadData];
        }];
        [self.collectionView reloadData];

    }
    return self;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
       SubmissionMinimalCollectionViewCell *cell = [self.collectionView 
           dequeueReusableCellWithReuseIdentifier:SubmissionCollectionCellIdentifier 
           forIndexPath:indexPath];
       [cell prepareForUseWithSubmission:[self.data objectAtIndex:indexPath.row]];
       cell.row  = indexPath.row;
       return cell;
}
mylegfeelsfunny
  • 507
  • 1
  • 8
  • 20
  • I've read it a couple of times and I still don't understand the question. – Abizern Jan 08 '14 at 20:05
  • So, when the collectionView reloads and `cellForItemAtIndexPath` gets called for `indexPath.row = 0`, `dequeueReusableCellWithReuseIdentifier` returns the cell that was previously used in `indexPath.row = 5`. Swapping the media causes an unwanted blip. I want the cell for `indexPath.row = 0` returned when `cellForItemAtIndexPath` is invoked and `dequeueReusableCellWithReuseIdentifier` is called for `indexPath.row = 0`. – mylegfeelsfunny Jan 11 '14 at 00:10
  • That's not hoe cells sure dequeued. Cells are recycled. You don't get the same cell back. If you want to prevent the blip, implement the `prepareForReuse` method and clear the cell ready f or the new data. – Abizern Jan 11 '14 at 00:34
  • The blip Im seeing is from `prepareForReuse`. If media url is the same, I don't reload it, if not I clear it. `UITableView` does recycle the cells in the order they came, I logged it out and it gives me the desired behavior. Its almost the exact same code for my `UITableViews` and it looks so much nicer. – mylegfeelsfunny Jan 11 '14 at 16:35
  • Okay, finally figured this out. It had nothing to to with the CollectionView. The image cache I am using was not as efficient as I thought it was. I was wrong. Thanks for the help anyway. – mylegfeelsfunny Jan 13 '14 at 19:36

0 Answers0