61

insertItemsAtIndexPathsで実行するとこのエラーが発生しますUICollectionView

でのアサーションの失敗:

-[UICollectionViewData indexPathForItemAtGlobalIndex:], 
/SourceCache/UIKit/UIKit-2372/UICollectionViewData.m:442
2012-09-26 18:12:34.432  
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'request for index path for global index 805306367 
when there are only 1 items in the collection view'

チェックしましたが、データソースに含まれている要素は1つだけです。これが発生する可能性がある理由についての洞察はありますか?より多くの情報が必要な場合、私は間違いなくそれを提供することができます。

4

14 に答える 14

69

最初のセルをコレクションビューに挿入するときに、これとまったく同じ問題が発生しました。UICollectionViewを呼び出すようにコードを変更することで、問題を修正しました

- (void)reloadData

最初のセルを挿入するときのメソッドですが、

- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths

他のすべてのセルを挿入するとき。

興味深いことに、私も問題を抱えていました

- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths

最後のセルを削除するとき。以前と同じことをしましたreloadData。最後のセルを削除するときに呼び出すだけです。

于 2012-10-19T18:08:56.527 に答える
11

セルを挿入する直前に section#0 を挿入すると、UICollectionView が満足するようです。

NSArray *indexPaths = /* indexPaths of the cells to be inserted */
NSUInteger countBeforeInsert = _cells.count;
dispatch_block_t updates = ^{
    if (countBeforeInsert < 1) {
        [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:0]];
    }
    [self.collectionView insertItemsAtIndexPaths:indexPaths];
};
[self.collectionView performBatchUpdates:updates completion:nil];
于 2013-03-18T23:17:13.903 に答える
3

私のコレクション ビューは 2 つのデータ ソースからアイテムを取得し、それらを更新するとこの問題が発生しました。私の回避策は、データの更新とコレクション ビューのリロードを一緒にキューに入れることでした。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

                //Update Data Array
                weakSelf.dataProfile = [results lastObject]; 

                //Reload CollectionView
                [weakSelf.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]];
 }];
于 2013-05-01T23:39:20.867 に答える
1

補助的なヘッダーまたはフッター ビュー (UICollectionViewFlowLayoutまたはそれから派生したレイアウト) を含むセクションにセルを挿入または移動し、挿入/移動前にセクションのセル数が 0 の場合に問題が発生するようです。

次のように、補足ヘッダー ビューを含むセクションに空の非表示のセルを配置することで、クラッシュを回避し、アニメーションを維持することしかできませんでした。

  1. - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)sectionヘッダー ビューがあるセクションの実際のセル `count + 1 を返すようにします。
  2. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPathお返しに

    if ((indexPath.section == YOUR_SECTION_WITH_THE_HEADER_VIEW) && (indexPath.item == [self collectionView:collectionView numberOfItemsInSection:indexPath.section] - 1)) {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EmptyCell" forIndexPath:indexPath];
    }
    

    ... その位置の空のセル。viewDidLoadUICollectionView を初期化する場所でセルの再利用を登録することを忘れないでください。

    [self.collectionView registerClass:[UICollectionReusableView class] forCellWithReuseIdentifier:@"EmptyCell"];
    
  3. クラッシュすることなく使用moveItemAtIndexPath:できます。insertItemsAtIndexPaths:
于 2013-04-22T16:01:43.837 に答える
1

私の場合、問題は作成方法でしたNSIndexPath。たとえば、3 番目のセルを削除するには、次のようにします。

NSIndexPath* indexPath = [NSIndexPath indexPathWithIndex:2];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];

私はする必要がありました:

NSIndexPath* indexPath = [NSIndexPath indexPathForItem:2 inSection:0];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
于 2016-03-18T16:38:39.260 に答える
1

私もこの問題に遭遇しました。これが私に起こったことです:

  1. 私は UICollectionViewController をサブクラス化しinitWithCollectionViewLayout:、 で を初期化していNSFetchedResultsControllerました。
  2. 共有クラスで NSURLConnection から結果を取得し、JSON 文字列を解析します (別のスレッド)
  3. フィードをループして my を作成し、NSManagedObjectsそれらを my に追加しますNSManagedObjectContext。これには、メイン スレッドNSManagedObjectContextparentContext.
  4. コンテキストを保存します。
  5. 変更をピックアップNSFetchedResultsControllerしてキューに入れます。
  6. - (void)controllerDidChangeContent:、変更を処理して自分の に適用しますUICollectionView

断続的に、OPが取得しているエラーが発生し、その理由がわかりませんでした。

この問題を解決するために、NSFetchedResultsController初期化をperformFetchメソッド- viewDidLoadに移動したところ、この問題はなくなりました。電話[collectionView reloadData]などをする必要はなく、すべてのアニメーションは適切に機能しています。

お役に立てれば!

于 2013-03-25T17:27:32.343 に答える
1

UICollectionViewDataSourceメソッドで正しい数の要素を返していることを確認します。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
于 2012-09-27T03:58:48.177 に答える
0

記録のために、私は同じ問題に遭遇しました.私にとっての解決策はヘッダーを削除することでした(.xibでそれらを無効にします).ヘッダーは不要になったため、このメソッドを削除しました. その後、すべてがうまくいくようです。

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementary
ElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
于 2013-04-26T13:36:56.550 に答える
0

私は自分でこの問題にぶつかりました。ここでのすべての回答は、Alex L を除いて問題があるように見えました。更新を参照することが答えのようでした。これが私の最終的な解決策です:

- (void)addItem:(id)item {
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (!_data) {
            _data = [NSMutableArray arrayWithObject:item];
        } else {
            [_data addObject:item];
        }
        [_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:_data.count-1 inSection:0]]];
    }];
}
于 2013-06-18T08:05:05.363 に答える