自定义UICollectionViewCell出错

Sin*_*nan 2 iphone cocoa-touch ios uicollectionview uicollectionviewcell

我正在尝试设置一个UICollectionView.起初我使用的是基本的UICollectionViewCell,它运行良好,细胞正在显示.

然后我创建了自己的collectionViewCell(子类化UICollectionViewCell),以便能够在单元格中显示图像.我将Storyboard中的自定义单元格与标识符相关联,并且还为其指定了正确的类.

但是,我一直有这个错误,我不明白为什么:

*** 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'
Run Code Online (Sandbox Code Playgroud)

我设法通过添加这一行来处理这个bug,viewDidLoad但在我关注的教程中,这些人没有使用它:

[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];
Run Code Online (Sandbox Code Playgroud)

问题是如果我添加它,collectionView将保持黑色并且不显示任何数据.

bob*_*ble 6

我昨天遇到了这个问题(因为文档说你必须注册该类),但是文档是不准确的,如果你的故事板中存在原型单元并且你给它一个标识符,你就不应该注册.

如果您UICollectionViewCell在故事板中创建了原型并分配了自定义单元类,则没有必要(如果您这样做则有害)注册自定义UICollectionViewCell.这就是产生UICollectionView黑色的原因.

根据错误消息,故事板中的原型单元格没有正确设置其标识符(根本不设置或不设置MediaCell)