UICollectionView visibleCells returns 0 before scrolling

I have a UICollectionView that implements lazy loading,

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

It works well. However, the problem is before scrolling, the first few cells show substitute images. Therefore, I am trying to call loadImagesToVisiableCells on a callback to retrieve the json file, which will be displayed in the UICollectionView below,

 - (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]; } 

A debug message is displayed before any scrolling, [[collectionView visibleCells] count] returns 0.

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

Any idea?

Relationship Hammer

+5
source share
1 answer

Thanks Rebooting the UICollectionView using the reloadData method returns immediately before reloading the data

The problem caused by the reboot is not completed. Decision:

 [collectionView reloadData]; **[self.collectionView layoutIfNeeded];** [self loadImagesToVisiableCells]; 
+15
source

Source: https://habr.com/ru/post/1203465/


All Articles