UICollectionView visibleCells在滚动之前返回0

时间:2014-09-26 08:51:31

标签: ios uicollectionview lazy-loading uicollectionviewcell

我有一个UICollectionView,我实现了延迟加载,

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
      [self loadImagesToVisiableCells];
}

效果很好。但是,问题是在任何滚动之前,第一个几个单元格显示占位符图像。所以我尝试在我的回调上调用loadImagesToVisiableCells来检索要在下面的UICollectionView中显示的json文件,

- (void)handleReceivedData: (NSArray*)results returnArrayOrDic:(NSNumber *)returnArrayOrDic{
    NSLog(@"***************************");
    NSLog(@"handleReceivedData has been called from PromotionViewController");
    NSLog(@"jsonArray Length:%d",[results count]);
    NSLog(@"jreturnArrayOrDic:%@",returnArrayOrDic);
    if([returnArrayOrDic boolValue] == YES){
      for(NSDictionary *dealDic in results) {
        NSLog(@"dealDic: %@", dealDic);
        //to populate the dealArray
        Deal *deal = [[Deal alloc] initWithDict:dealDic];
        [dealArray addObject:deal];
      }
      NSLog(@"%d deals have been populated to dealArray",[dealArray count]);
    }else{
      NSDictionary *jsonDictionary = (NSDictionary *)results;
      for(id key in jsonDictionary) {

        id value = [jsonDictionary objectForKey:key];

        NSString *keyAsString = (NSString *)key;
        NSString *valueAsString = (NSString *)value;

        NSLog(@"key: %@", keyAsString);
        NSLog(@"value: %@", valueAsString);
      }
    }
    [self.collectionView reloadData];
    [self loadImagesToVisiableCells];
}

在任何滚动之前显示调试消息,[[collectionView visibleCells] count]返回0.

    - (void)loadImagesToVisiableCells{
        NSLog(@"starting loadImagesToVisiableCells, visibleCells:%d",[[collectionView visibleCells] count]);
....
}

有什么想法吗?

此致 锤

1 个答案:

答案 0 :(得分:21)

感谢Reloading a UICollectionView using reloadData method returns immediately before reloading data

问题是由重装尚未完成引起的。解决方案是,

[collectionView reloadData];
**[self.collectionView layoutIfNeeded];**
[self loadImagesToVisiableCells];