2

をセットアップしようとしていますUICollectionView。最初はbasicを使ってUICollectionViewCellいましたが、うまく動いていて、セルが表示されていました。

UICollectionViewCell次に、セルに画像を表示できるように、独自の collectionViewCell (サブクラス化) を作成しました。ストーリーボードのカスタム セルを識別子にリンクし、適切なクラスも指定しました。

ただし、このエラーが引き続き発生し、理由がわかりません。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MediaCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

にこの行を追加することでバグに対処することができましたviewDidLoadが、チュートリアルでは、私がフォローしていた人はそれを使用していませんでした:

[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];

問題は、追加すると、collectionView が黒のままで、データが表示されないことです。

4

3 に答える 3

2

あなたはその線を使うのが正しい

[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"]; 

これはセルの再利用に役立ちます。

問題は、cellForRowAtIndexPathメソッド、サブクラス、またはデリゲートの設定方法にある可能性があります。コードを投稿できますか?

デリゲート メソッドは次のようになります。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MediaCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MediaCell" forIndexPath:indexPath];
    // set cell.image
    return cell;
}

viewDidLoad メソッドでデリゲートを設定する必要があります...

[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];

編集: MediaCollectionViewCell 実装のコードを投稿できますか? 私は似たようなことをしました、そして私のものはこのように見えます。

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [self.contentView addSubview:self.imageView];
}
return self;

}

于 2012-12-24T11:09:20.180 に答える
1

これは、実際には TabBarController に関連する問題でした。その中に正しいクラスを表示していなかったので、collectionView は常に空でした。ここに投稿したコードは機能していました。皆様、ご協力ありがとうございました。

于 2013-01-08T15:30:35.043 に答える