0

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];
}
}
4

1 回答 1

2

首先删除对象表单数组,然后从 CollectionView 代码中删除项目:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _totalImagesData.count;   //array from which you are displaying data in collection view
} 


//delete
 [_totalImagesData removeObjectAtIndex:indexPath.item];
 [self.collectionViewPack deleteItemsAtIndexPaths:[NSArray arrayWithObject:indPath]];
于 2013-09-04T12:32:56.123 回答