6

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 "count" log statement there shows that it returns 5. However, this time, cellForItemAtIndexPath is never called, and the UI still shows the three cells from the first load.

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

theraju
  • 461
  • 4
  • 13
  • Perhaps this answer might help: http://stackoverflow.com/a/19100840/2274694 – Lyndsey Scott Dec 07 '14 at 04:20
  • UICollectionView & UITableView only update visible cells in view. – NANNAV Dec 07 '14 at 05:13
  • If i try what Lyndsey suggested it complains that I have changed the count without inserting and deleting items: 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0.  The number of items contained in an existing section after the update (5) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' – theraju Dec 07 '14 at 05:41
  • I'm gonna look at performBatchUpdates to clear the data source and insert the new content – theraju Dec 07 '14 at 05:49

3 Answers3

1

Try setting the datasource and delegate again for the UICollectionView and then reloading it again.

-(void)reloadCollectionView {
  mycollectionView.dataSource = self;
  mycollectionview.delegate = self;
  [mycollectionview reloadData];
}
Nathan Tuggy
  • 2,239
  • 27
  • 28
  • 36
  • Nope same NSInternalInconsistencyException. Wondering if its this issue: http://stackoverflow.com/questions/12611292/uicollectionview-assertion-failure – theraju Dec 08 '14 at 07:36
1

I think, may be you are using the concept of static collection view cell. If its static, change it to dynamic in your xib/Storyboard.

iDevAmit
  • 1,312
  • 1
  • 19
  • 31
0

Please check you have connected both datasource and delegate of collectionView to your view controller. if not do this

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

Hope this will help you.

Ajith Kumar
  • 1,163
  • 1
  • 9
  • 21