Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView reloadData But View Blinks

I have an UICollectionView and a custom UICollectionViewCell to display my content, which should be refreshed every second.

I am going to invoke reloadData method to reload the whole collection view to fit my needs.

But, but, My collection view cell blinks every time I reload data.

It seems like the image below. Two seconds of my app. The first second is OK. But second second, the collection view display reused cell first (yellow area) and then display the correct cell(configured cell) finally. Which looks like a blink.

enter image description here

It seems like a cell-reuse issue. CollectionView displays cells without completely finish configure it. How could I fix it?

My cellForItemAtIndexPath: method:

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

    YKAnimeDownloadTask *animeDownloadTask = [[YKVideoDownloadTaskManager shared] animeDownloadTasks][indexPath.row];
    [cell setUpWithDownloadAnime:animeDownloadTask editing:self.vc.isEditing];
    cell.delegate = self;

    if (self.vc.isEditing) {
        CABasicAnimation *imageViewAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        imageViewAnimation.fromValue = @(-M_PI/64);
        imageViewAnimation.toValue = @(M_PI/128);
        imageViewAnimation.duration = 0.1;
        imageViewAnimation.repeatCount = NSUIntegerMax;
        imageViewAnimation.autoreverses = YES;
        [cell.coverImageView.layer addAnimation:imageViewAnimation forKey:@"SpringboardShake"];

        CABasicAnimation *deleteBtnAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        deleteBtnAnimation.fromValue = @(-M_PI/32);
        deleteBtnAnimation.toValue = @(M_PI/32);
        deleteBtnAnimation.duration = 0.1;
        deleteBtnAnimation.repeatCount = NSUIntegerMax;
        deleteBtnAnimation.autoreverses = YES;
        [cell.deleteBtn.layer addAnimation:deleteBtnAnimation forKey:@"SpringboardShake1"];
    } else {
        [cell.coverImageView.layer removeAllAnimations];
        [cell.deleteBtn.layer removeAllAnimations];
    }

    return cell;
}
like image 888
Cao Dongping Avatar asked May 19 '14 14:05

Cao Dongping


2 Answers

Well I found the answer though it's been so long from the question.

Don't use reloadData() nor reloadItems().

Use collectionView.reloadSections(IndexSet(integer: yourSectionIndex)). The animation will happen smoothly.

like image 126
Arman Momeni Avatar answered Nov 13 '22 10:11

Arman Momeni


self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)

Helped for me.

like image 32
LembergSun Avatar answered Nov 13 '22 11:11

LembergSun