我的应用程序包含一个UICollectionView
为卡片匹配游戏显示许多自定义子视图的应用程序。基本上,当三张卡片匹配时,包含它们的单元格将从集合视图中删除。当所有三个单元格都在可滚动屏幕上可见时,我的删除功能才起作用,但当一两个单元格不在屏幕上时则不起作用。发生的情况是屏幕上的单元格将被删除,但是当我尝试向上滚动以查看其他单元格(也应该被删除)时,应用程序崩溃了。
以下是我的卡更新功能的代码:
- (void) updateCell: (SetCardCell*) cell withCard: (SetsCard *) cardInModel {
if ([cell isKindOfClass: [SetCardCell class]]){
if ([cell.setCardView isKindOfClass:[SetCardView class]]) {
// various actions to match view with model
SetCardView *cardView = cell.setCardView;
[cardView setNeedsDisplay];
// do things to UI if card is faced up or unplayable
if (!cardInModel.isUnplayable) {
if (cardInModel.isFaceUp) {
cell.setCardView.alpha = 0.3;
} else {
cell.setCardView.alpha = 1;
}
} else {
// remove the cell - this is where the problem is
NSLog(@"%@", cell.description); ** returns a cell **
NSLog(@"%@", [self.collectionView indexPathForCell:cell].description); ** returns (null) when the cell is offscreen, but a normal index path if otherwise **
[self.game.cards removeObjectsInArray:@[cardInModel]];
[self.collectionView deleteItemsAtIndexPaths:@[[self.collectionView indexPathForCell:cell]]];
}
}
}
}
有想法该怎么解决这个吗?非常感谢你!
编辑:我忘记了一条错误消息,如下所示:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'