29

エラーが発生しています...

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249

UICollectionView を表示しようとしたとき。

それを引き起こす行は...

static NSString *CellIdentifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

デキューでエラーが発生しました。

他にエラーはないので、どこから始めればよいかわかりません。

誰でもこれに光を当てることができますか?

4

7 に答える 7

37

以下のように登録する必要があります。

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
于 2012-11-10T10:53:43.790 に答える
29

ドキュメントを読んでいました(おそらくこれを最初に行うべきでした:))

とにかく、私が使用しているcollectionViewは、別のxibファイル(ストーリーボードではない)内にあり、ドキュメントから...

Important: You must register a class or nib file using the
registerClass:forCellWithReuseIdentifier: or
registerNib:forCellWithReuseIdentifier: method before calling this method.

ありがとう

于 2012-09-26T10:48:34.077 に答える
4

私も同じ問題を抱えていました。これが私がそれを解決した方法です。

動く

[self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

にいるために- (void)viewDidLoad

むしろ方法- (void)awakeFromNib

于 2016-06-05T10:00:58.897 に答える
3

registerNib:メソッドを使用する場合は、次のことを確認してください。

UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil];
[collectionView registerNib:nibH
 forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
        withReuseIdentifier:HEADER_ID];

再利用可能な最上位コレクションのビューを選択するときは、nib ファイルでもそれを確認し、属性インスペクターを使用し、パラメーターに渡す値と同じ値に設定されていることを確認してください。IdentifierwithReuseIdentifier:

于 2013-02-05T15:58:36.263 に答える
0

一意の ReuseIdentifier を持つ複数の UICollectionView を使用すると、このエラーがポップアップするのを見てきました。ViewDidLoad では、各 CollectionView の reuseIdentifier を次のように登録します。

[_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"];
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"];

次に、「- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath」に到達したら、collectionView1 のセルを collectionView2 の reuseIdentifier に設定しようとしないか、またはこのエラーが発生します。

これを行わないでください : (または、collectionView2 が間違った識別子を参照し、予期していた識別子を参照する前にフィットをスローします)

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];

if(collectionView != _collectionView1){
   cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}

cell.backgroundColor = [UIColor greenColor];
return cell;

これを行います:

UICollectionViewCell *cell;

if(collectionView == _collectionView1){
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
}else{
   cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}

cell.backgroundColor = [UIColor greenColor];
return cell;
于 2015-04-15T13:20:31.880 に答える
0

交換

NSString *CellIdentifier = @"Cell";

static NSString *CellIdentifier = @"Cell";
于 2012-09-26T10:41:03.927 に答える