0

If the model data in an NSArray does not change, and you call reloadData on the UICollectionView, do the cells appear in different spots?

I'm finding that if my NSArray holds Obj1, Obj2, Obj3, then at first, the UICollectionView correctly maps Obj1 to the first collectionViewCell, Obj2 to the second, etc. This is how it's set up in the cellForRowAtIndexPath, as it uses indexPath.row to get the right Obj from the array.

However, when I call reloadData, all the objects are mapped to the wrong UICollectionViewCell (Obj1 ends up somewhere else, such as the second collectionViewCell, and Obj2 ends up as the third collectionViewCell, etc.)

Why does this happen? I didn't find the reason in the documentation.


FURTHERMORE, I'm having the same problem when my model array does change. For example, if my NSArray is Obj1, Obj2, Obj3, and then new instances of the Objs are created and are now in a different order, such that the NSArray is Obj2, Obj3, Obj1... and then I call reloadData, I face the same problem.

Obj2 doesn't map to the first UICollectionViewCell as it should, but instead mapped to a different UICollectionViewCell

Anyone know why this happens with UICollectionViews?

Thanks!

EDIT: Code for cellForRowAtIndexPath

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MIAppCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MIAppCell" forIndexPath:indexPath];
// set model data
[cell setAppTitle:[(MIAppDO*)[self.apps objectAtIndex:indexPath.row] getAppTitle]];
return cell;
}
Dalee Davis
  • 981
  • 8
  • 19
Rohan Agarwal
  • 2,167
  • 1
  • 24
  • 34
  • Show the code you're using for collectionView:cellForItemAtIndexPath: – rdelmar Jan 04 '13 at 03:48
  • I don't see anything wrong in this code. You might try logging indexPath and [(MIAppDO*)[self.apps objectAtIndex:indexPath.row] getAppTitle], and see if they're giving you something you don't expect. – rdelmar Jan 04 '13 at 03:55
  • I tried logging both already. They give what's expected, for the first cell, the indexPath.row is 0 and the getAppTitle comes from the first object (index 0) of the array. However, the UICollectionViewCell doesn't show the right title, it shows the title of another object elsewhere in the array. – Rohan Agarwal Jan 04 '13 at 03:58
  • I haven't seen this behavior before, so I'm at a loss to explain it. You might want to post the code in your custom cell. – rdelmar Jan 04 '13 at 04:25
  • Thanks for pointing me in the direction of my custom cell, I figured out the answer – Rohan Agarwal Jan 04 '13 at 04:34

1 Answers1

0

I figured out the problem.

Earlier, I was dequeuing a cell and setting it's new model data, but the model data wasn't correctly reflecting in the subclassed UICollectionViewCell.

I needed to override the setter methods for my properties that represent the model data for my subclassed UICollectionViewCell. In those overriden methods, I was able to update the appropriate views of the cell.

Dalee Davis
  • 981
  • 8
  • 19
Rohan Agarwal
  • 2,167
  • 1
  • 24
  • 34