UICollectionViewCells dequeued in wrong order

1.2k Views Asked by At

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;
}
0

There are 0 best solutions below