2013-05-07 1 views
3

вот вызов для всех вас ...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; 
} 

Как вы можете видеть, это довольно просто.

Здесь приходит странное поведение: если я ставлю все мои пользовательские UICollectionViewCell только в одном разделе UICollectionView, производительность хорошо ...

какие-либо идеи?

Дополнительная информация: UICollectionView есть заголовки. Пользовательские заголовки. Просто UIView wit 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

Вы должны использовать Инструменты и Профайлер времени, чтобы определить, где тратится время. –

+0

Ну, Time Profiler говорит, что проблема здесь: UserPhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: @ «userPhotoCell» forIndexPath: indexPath]; ... ReusableCell ... возможно, я должен включить «if (cell == nil)», ? –

ответ

14

Я думаю, что нашел ответ. По какой-то странной причине [collectionView reloadData] не запускался в основном потоке. Таким образом, мое решение так просто:

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

Теперь, UICollectionView обновляет сразу, как хотелось бы.

Некоторые примечания: этот [collectionView reloadData] был вызван в методе делегата из пользовательского класса с использованием AFNetworking после использования очередей. Надеюсь, поможет.

+0

wow..общательно полезный..отличный мужчина .. спасибо за вашу помощь ... –

+0

добро пожаловать! –