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;
}
I figured out the problem.
Earlier, I was
dequeuinga cell and setting it’s new model data, but the model data wasn’t correctly reflecting in the subclassedUICollectionViewCell.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.