I keep getting an assertion failure when I try to delete cells from my UICollectionView. What I am doing a card game and when the cards match they are to be deleted. I made it so when they match I delete all the matched cards in the model and then delete them from the CollectionView. This is part of Assignment #3 from stanford cs193p course. Here is my code.
Controller Code
-(void) updateUI{
NSMutableArray* tempCardToDelete = [[NSMutableArray alloc] init];
for(UICollectionViewCell *cell in [self.cardCollectionView visibleCells]){
NSIndexPath *indexPath = [self.cardCollectionView indexPathForCell:cell];
NSLog(@"%d", indexPath.item);
Card* card = [self.game cardAtIndex:indexPath.item];
NSLog(@"Face up is %d", card.faceUp);
NSLog(@"%@", card.contents);
// This will update the individual cell with a card value
[self updateCell:cell usingCard:card];
//cards that are not playable and faceup have been matched so we store them
if(card.isFaceUp && card.isUnplayable){
[tempCardToDelete addObject:indexPath];
self.cardsToDelete = [tempCardToDelete copy];
}
}
//delete the cards from the model
[self.game deleteMatchedCards];
//delete the cards from the collectionView
if(self.cardsToDelete.count != 0){
[self.cardCollectionView deleteItemsAtIndexPaths:self.cardsToDelete];
}
}
Model Code
-(void) deleteMatchedCards{
//the property stores the cards that should be deleted
for(Card *cards in self.modelCardsToDelete){
NSUInteger deleteIndex = [self.modelCardsToDelete indexOfObject:cards];
[self.cards removeObjectAtIndex:deleteIndex];
}
}