0

私のアプリはUICollectionView、カード マッチング ゲームの多数のカスタム サブビューを表示する で構成されています。基本的に、3 枚のカードが一致すると、それらを含むセルがコレクション ビューから削除されます。私の削除機能は、スクロール可能な画面に 3 つのセルすべてが表示されている場合は機能しますが、1 つまたは 2 つが画面外にある場合は機能しません。何が起こるかというと、画面上のセルは削除されますが、他のセル (同様に削除されるはずです) を上にスクロールしようとすると、アプリがクラッシュします。

以下は、私のカード更新機能のコードです。

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

4

2 に答える 2

0

ご覧のとおり、 を使用すると、現在表示されていないセルのインデックス パスが nil になりますindexPathForCell。これにより、配列から配列を作成して削除メソッドに渡そうとすると、クラッシュが発生します。delete メソッド自体はクラッシュしていません。配列を作成しているだけです。

セルを使用して更新を行うべきではありません。できるだけ早くモデル内のオブジェクトへの参照を取得し、インデックス パスを導き出し、そこから削除を実行する必要があります。セルは、モデルの表現の一部にすぎません。

コードを見ると、可能な修正は、配列内の の位置からインデックス パスを取得cardInModelすることです (削除する前に?)

于 2013-06-17T06:36:11.243 に答える