Reputation: 1034
I am using a collection view to display 'cards'. When the user clicks one of these cards, the view changes with a flip animation (to the other 'side' - via a function called in didSelectItemAtIndexPath). If a user touches another one of these cards, they can click another and the old card will flip back to the old view, and the new card will flip to the new view. Alternatively they can click the original card again and flip it back.
The problem
This works great except for the fact that, when scrolling to the cards in the scroll view other cards appear to be flipped. For example, I flip the first card in the collection view (index: 0). This flips fine. I scroll down, and the cell of index 9 appears flipped.
What I have tried
I have looked around, and although I can see plenty of issues to do with UICollectionView and scrolling, I cant figure out whats the same and what isn't. I have also used NSLog to determine if the randomly flipped view cards have been selected and flipped via the program, and they havent. They just appear to be.
Im really stuck for ideas with this. Anybody got any ideas? It it due to the way im creating the cells? In my viewDidLoad I am using: (where cellImage is my custom class for the cells)
[self.collectionView registerClass:[cellImage class] forCellWithReuseIdentifier:@"MY_CELL"];
and in my cellForItemAtIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
cellImage *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.item]];
[cell.label addSubview:cell.imageView];
cell.label.backgroundColor = [UIColor whiteColor];
cell.label.layer.borderColor = [UIColor grayColor].CGColor;
cell.label.layer.borderWidth = 1.0f;
return cell;
}
Does anybody have any ideas? If you need any more code just give me a shout, thanks!
Upvotes: 0
Views: 1006
Reputation: 1625
You could override the - (void)prepareForReuse
method of your custom UICollectionViewCell to return the cell to a default state before reusing it.
Upvotes: 0
Reputation: 80265
In the method to configure your reusable cell (cellForItemAtIndexPath
), you have to explicitly set both states. Otherwise the the recycled cell will not update itself.
image = isFlipped ? backImage : cardImage;
You should have a data model that can reliably return the current flipped state of any card in your array.
Upvotes: 1