Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView刷新数据需要很长时间_Ios_Uicollectionview_Reloaddata - Fatal编程技术网

Ios UICollectionView刷新数据需要很长时间

Ios UICollectionView刷新数据需要很长时间,ios,uicollectionview,reloaddata,Ios,Uicollectionview,Reloaddata,这是对你们所有人的挑战 我的UIViewController内有一个UICollectionView开关正在正确加载。 我还有一个自定义的UICollectionViewCell类,其中包含一个UIButton 我使用一些UIImage对象从服务器检索NSArray,以便为自定义UICollectionViewCell的按钮分配一个背景图像 下面是我的cellForItemAtIndexPath函数的代码: - (UICollectionViewCell *)collectionView:(UI

这是对你们所有人的挑战

我的
UIViewController
内有一个
UICollectionView
开关正在正确加载。 我还有一个自定义的
UICollectionViewCell
类,其中包含一个
UIButton

我使用一些
UIImage
对象从服务器检索
NSArray
,以便为自定义
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;
}
正如你所看到的,这很简单

这是一个奇怪的行为:如果我将所有自定义的
UICollectionViewCell
放在
UICollectionView
的一个部分中,性能就可以了

有什么想法吗

一些额外信息:
UICollectionView
有标题。自定义标头。此时只需一个带有
ui标签的
ui视图

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

我想我已经找到了答案。出于某种奇怪的原因,
[collectionView reloadData]
没有在主线程上触发。因此,我的解决方案非常简单:

dispatch_async(dispatch_get_main_queue(), ^{
        [_collectionUserPhotos reloadData];
    });
现在,UICollectionView会根据需要立即更新


一些注意事项:此
[collectionView reloadData]
是在使用队列之后,在自定义类的委托方法中调用的。希望有帮助。

您应该使用工具和时间分析器来确定时间花在哪里。时间分析器说问题就在这里:UserPhotoCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“UserPhotoCell”forindexath:indexPath];。。。可重用单元。。。也许我应该包括“if(cell==nil)”?