2013-06-24 112 views
0

當我嘗試從我的UICollectionView中刪除單元格時,我不斷收到斷言失敗。我正在做一個紙牌遊戲,當紙牌匹配時,他們將被刪除。我做到了,所以當它們匹配時,我刪除模型中所有匹配的卡片,然後從CollectionView中刪除它們。這是stanford cs193p課程作業#3的一部分。這是我的代碼。刪除時在UICollectionView中聲明失敗

控制器代碼

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

什麼是modelCardsToDelete定義爲?它應該和cardsToDelete一樣嗎? – davis

+0

你得到的是什麼錯誤? – rmaddy

+0

你的代碼中的斷言是斷言失敗嗎? (如果是這樣,它出現在哪裏) –

回答

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