2013-05-07 37 views
3

这里是为大家的挑战......UICollectionView需要很长的时间来刷新数据

我有一个UICollectionViewUIViewController女巫内部正确加载。 我也有一个自定义UICollectionViewCell职业女巫包含一个UIButton

我为了一个背景图像分配给我的自定义UICollectionViewCell的按钮来检索从我的一些UIImage对象服务器NSArray

这是我的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; 
} 

正如你所看到的是很简单的。

来了奇怪的行为:如果我把我所有的自定义UICollectionViewCellUICollectionView只是一个部分,其性能是好的...

什么想法?

一些额外的信息: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; 
} 
+0

您应该使用仪器和时间分析器来确定时间花在哪里。 – 2013-05-07 12:59:16

+0

嗯,时间事件探查器说这个问题在这里:UserPhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@“userPhotoCell”forIndexPath:indexPath]; ... reusableCell ...也许我应该包括“if(cell == nil)” ? – 2013-05-07 13:11:23

回答

14

我想我找到了答案。由于一些奇怪的原因,[collectionView reloadData]没有在主线程中被触发。所以,我的解决方案如此简单:

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

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

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

+0

哇..真的很有帮助..真棒男人..感谢您的帮助... – 2013-10-30 06:53:45

+0

不客气! – 2013-10-31 19:27:40

相关问题