UICollectionView visibleCells在滚动之前返回0

Ham*_*mer 6 lazy-loading ios uicollectionview uicollectionviewcell

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

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
      [self loadImagesToVisiableCells];
}
Run Code Online (Sandbox Code Playgroud)

它运作良好.但是,问题是在任何滚动之前,第一个几个单元格显示占位符图像.所以我尝试在我的回调上调用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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

任何的想法?

关心锤子

Ham*_*mer 22

感谢 使用reloadData方法重新加载UICollectionView,然后在重新加载数据之前立即返回

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

[collectionView reloadData];
**[self.collectionView layoutIfNeeded];**
[self loadImagesToVisiableCells];
Run Code Online (Sandbox Code Playgroud)