UICollectionView需要很长时间才能刷新数据

Jor*_*mos 3 reloaddata ios uicollectionview

这对你们所有人来说都是挑战......

我有一个UICollectionView内部我的UIViewController女巫正确加载.我也有一个定制UICollectionViewCell类女巫包含一个UIButton.

NSArray从我的服务器检索一些UIImage对象,以便将一个背景图像分配给我的自定义按钮UICollectionViewCell.

这是我的cellForItemAtIndexPath功能代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

你可以看到很简单.

这有一个奇怪的行为:如果我把我的所有习惯都UICollectionViewCell放在其中的一个部分UICollectionView,那么性能还可以......

有任何想法吗?

一些额外的信息:UICollectionView有标题.自定义标题.这只是一个UIView机智UILabel.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

Jor*_*mos 14

我想我找到了答案.由于一些奇怪的原因,[collectionView reloadData]没有在主线程上被解雇.所以,我的解决方案很简单:

dispatch_async(dispatch_get_main_queue(), ^{
        [_collectionUserPhotos reloadData];
    });
Run Code Online (Sandbox Code Playgroud)

现在,UICollectionView会根据需要立即更新.

一些注意事项:在使用队列之后,[collectionView reloadData]使用AFNetworking从自定义类的委托方法中调用此方法.希望能帮助到你.