我正在尝试设置一个 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'
我设法通过在 viewDidLoad
中添加这一行来解决这个错误,但是在我关注的教程中,这些人没有使用它:
[self.collectionView registerClass:[MediaCollectionViewCell class] forCellWithReuseIdentifier:@"MediaCell"];
问题是,如果我添加它,collectionView 会保持黑色并且不会显示任何数据。
最佳答案
我昨天遇到了这个确切的问题(因为文档说你必须注册这个类),但是文档是不准确的,因为如果你的 Storyboard中存在原型(prototype)单元并且你给它一个标识符,你不应该注册。
如果您在 Storyboard中创建了 UICollectionViewCell
的原型(prototype)并分配了自定义单元格类,则没有必要(如果这样做是有害的)注册自定义 UICollectionViewCell
。这就是生成的 UICollectionView
为黑色的原因。
根据错误消息, Storyboard中的原型(prototype)单元格没有正确设置其标识符(根本没有设置或不是 MediaCell
)
关于iphone - 自定义 UICollectionViewCell 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14020221/