UICollectionView don't reuse cell on ios 8 and ipad air 1

234 Views Asked by At

I'm not using storyboard, everything is done by code.. and when I scroll the UICollectionView.. after it reusing correctly..some cells.. than it happen :

-the cell initWithFrame is being call

-new gray hair appear on my head.

I read other q/a and check maybe it's something with threads but all the reloadData is on the main thread.

any directions ?

1

There are 1 best solutions below

3
On

I have no idea what's your code, so I'll propose how do I do it:

// somewhere in eg viewDidLoad
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellId];

And later the delegate:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = cell = [collectionView dequeueReusableCellWithReuseIdentifier:MyCellId forIndexPath:indexPath];

    // do something with your cell, set title or anything

    return cell;
}

There is another possibility. Your cell, as it's reusable, will have already saved previous properties. So if you did something like this:

if (iCanAddGrayHair) {
    [cell.imageView setImage:[UIImage imageNamed:@"hair"]]
}

Then do notice, that new cell will still have this image set! You need to add:

else {
    [cell.imageView setImage:nil];
}

To reset it from previous state. You can also override prepareForReuse: method of UICollectionViewCell class to reset the values (don't forget to call super).