Swift - 无法出现类型的视图:带标识符的UICollectionElementKindCell [英] Swift - could not dequeue a view of kind: UICollectionElementKindCell with identifier
问题描述
我在尝试加载UICollectionView时收到此错误消息。
I got this error message when trying to load UICollectionView.
2015-07-23 16:16:09.754 XXXXX [24780: 465607] *由于
未捕获异常'NSInternalInconsistencyException'而终止应用程序,原因:'$
不会出现类型的视图:UICollectionElementKindCell带有
标识符CollectionViewCell - 必须注册一个笔尖或一个
标识符的类或连接故事板中的原型单元'
* 第一次抛出调用堆栈:
2015-07-23 16:16:09.754 XXXXX[24780:465607] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack:
我的代码
@IBOutlet var collectionView: UICollectionView!
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "category")
return cell
}
我已经在故事板检查器中声明了 CollectionViewCell
,但仍然出现错误消息。
I already declared CollectionViewCell
in storyboard inspector but the error message still occur.
请咨询。谢谢。
推荐答案
看了你的例外之后:
2015-07-23 16:16:09.754 XXXXX [24780:465607] *由于
未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'$
不会出列视图类型:UICollectionElementKindCell与
标识符CollectionViewCell - 必须为
标识符注册一个nib或类或在故事板中连接原型单元'*首先抛出
调用堆栈:
2015-07-23 16:16:09.754 XXXXX[24780:465607] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack:
最后一部分是最重要的:
last part is most important:
必须注册一个nib或
标识符的类或连接故事板中的原型单元格
must register a nib or a class for the identifier or connect a prototype cell in a storyboard
这意味着您的集合视图不会已注册您的自定义单元格。要在 viewDidLoad
中添加以下内容:
Which means that your collection view doesn't have registered your custom cell. To resolve this add following in your viewDidLoad
:
var nib = UINib(nibName: "UICollectionElementKindCell", bundle:nil)
self.collectionView.registerNib(nib, forCellReuseIdentifier: "CollectionViewCell")
这篇关于Swift - 无法出现类型的视图:带标识符的UICollectionElementKindCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!