0

UICollectionView からセルを削除しようとすると、アサーション エラーが発生し続けます。私がカードゲームをしていて、カードが一致するとそれらは削除されます。それらが一致すると、モデル内の一致したすべてのカードを削除してから、それらを CollectionView から削除するようにしました。これは、スタンフォード cs193p コースの課題 #3 の一部です。これが私のコードです。

コントローラーコード

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

}

型式コード

-(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