删除时UICollectionView中的断言失败

时间:2013-06-24 20:34:45

标签: objective-c uicollectionview assertion cs193p

当我尝试从我的UICollectionView中删除单元格时,我一直收到断言失败。我正在做一个纸牌游戏,当卡片匹配时,他们将被删除。我在匹配时就这样做了我删除了模型中所有匹配的卡片,然后从CollectionView中删除它们。这是stanford 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];
}
}

1 个答案:

答案 0 :(得分:2)

首先从数组中删除对象,然后从Collection View中删除项目 代码:

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