2013-06-24 2 views
0

Я продолжаю получать отказ от утверждения при попытке удалить ячейки из моего UICollectionView. Что я делаю в карточной игре, и когда карты совпадают, они должны быть удалены. Я сделал это, когда они совпадают. Я удаляю все сопоставленные карты в модели, а затем удаляю их из CollectionView. Это часть задания № 3 от курса stanford cs193p. Вот мой код.Сбой утверждения в UICollectionView при удалении

Controller Код

-(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 определяется как? Предполагается, что он будет таким же, как cardToDelete? – davis

+0

Какова полная ошибка, которую вы получаете? – rmaddy

+0

Является ли отказ утверждения от «assert» в вашем коде? (И если да, то где это появляется) –

ответ

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