ios - UICollectionView 刷新数据时间长

标签 ios uicollectionview reloaddata

这是给你们所有人的挑战......

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

我从我的服务器中检索了一个带有一些 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 有标题。自定义 header 。此时只有一个 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;
}

最佳答案

我想我已经找到了答案。由于某些奇怪的原因,[collectionView reloadData] 没有在主线程上被触发。所以,我的解决方案就这么简单:

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

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

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

关于ios - UICollectionView 刷新数据时间长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16419645/

相关文章:

iphone - 在 iPhone 的 UIPickerView 中将 UITextField 的文本设置为默认值。?

ios - Xcode:左上角和右上角半径不起作用

ios - UICollectionView Xamarin.iOS,带按钮的自定义单元格

ios - swift 中 UICollectionView 单元格内的 UITextField 值

ios - 动画 [tableView reloadData]

iOS 应用程序上传到 iTunes Connect 导致无效签名问题

ios - 'int' 到 'NSString*' 的隐式转换

Swift:如何一次启用或禁用嵌入自定义类中的多个按钮?

iphone - 如果当前 View 是 tableViewController,iOS TableView 不会重新加载

arrays - 值更改后 CollectionView reloaddata()