UICollectionView: reload data calls numberOfItemsInSection but not cellForItemAtIndexPath

I have a simple UICollectionView. The first time the view is loaded, cellForItemAtIndexPath is called three times, and I see three cells.

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { NSUInteger count = ([self.results count] > 0 ? [self.results count] : 3); NSLog(@"count %lu", (unsigned long)count); return count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cellForItemAtIndexPath called"); UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"TasteCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; return cell; } 

However, later self.results becomes an array of 5 elements, and I call:

 [self.collectionView reloadData]; 

When this happens, numberOfItemsInSection is called again, and the counter log statement says that it returns 5. However, this time cellForItemAtIndexPath is never called, and the user interface still shows three cells from the first file loaded.

Why is the number of cells not updated even if the count has changed?

+6
source share
3 answers

Try installing the data source again and delegating it to the UICollectionView, and then reloading it again.

 -(void)reloadCollectionView { mycollectionView.dataSource = self; mycollectionview.delegate = self; [mycollectionview reloadData]; } 
+1
source

I think maybe you are using the concept of a static collection. If its static, change it to dynamic in xib / Storyboard.

+1
source

Verify that you have connected both the data source and the delegate of the View collection to your view controller. if you do not do this

  -(void)viewDidLoad { [super viewDidLoad]; mycollectionView.dataSource = self; mycollectionview.delegate = self; } 

Hope this helps you.

0
source

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


All Articles