Jorge Ramos
Jorge Ramos

Reputation: 891

UICollectionView takes a long time to refresh data

here's a challenge for you all...

I have an UICollectionView inside my UIViewController witch is loading correctly. I also have a custom UICollectionViewCell class witch contains an UIButton.

I retrieve a NSArray from my server with some UIImage objects in order to assign one background image to the button of my custom UICollectionViewCell.

Here's the code of my cellForItemAtIndexPath function:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    UserPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"userPhotoCell" forIndexPath:indexPath];

    if (indexPath.section == 0) {
        [[cell imageButton] setBackgroundImage:[userPublicImages objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    } else {
        [[cell imageButton] setBackgroundImage:[userPrivateImages objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    }

    return cell;
}

As you can see is quite simple.

Here comes the strange behavior: if I put all my custom UICollectionViewCell in just one section of the UICollectionView, the performance is okay...

any ideas?

Some extra information: The UICollectionView have headers. Custom headers. Just an UIView wit an UILabel at this moment.

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeader" forIndexPath:indexPath];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [headerView frame].size.width, 40.0f)];
        if (indexPath.section == 0) {
            [titleLabel setText:NSLocalizedStringFromTable (@"collectionTitle_publicPhotos", [[NSLocale preferredLanguages] objectAtIndex:0] , @"")];
        } else {
            [titleLabel setText:NSLocalizedStringFromTable (@"collectionTitle_privatePhotos", [[NSLocale preferredLanguages] objectAtIndex:0] , @"")];
        }
        [headerView addSubview:titleLabel];

        reusableView = headerView;
    }

    return reusableView;
}

Upvotes: 3

Views: 3099

Answers (1)

Jorge Ramos
Jorge Ramos

Reputation: 891

I think I've found the answer. For some strange reason, the [collectionView reloadData] was not being fired on main thread. So, my solution is as simple as that:

dispatch_async(dispatch_get_main_queue(), ^{
        [_collectionUserPhotos reloadData];
    });

Now the, UICollectionView updates immediately, as desired.

Some notes: this [collectionView reloadData] was called in a delegate method from a custom class using AFNetworking, after using queues. Hope it helps.

Upvotes: 14

Related Questions