斯威夫特 - 无法出列一种视图:带有标识符的 UICollectionElementKindCell [英] Swift - could not dequeue a view of kind: UICollectionElementKindCell with identifier

查看:18
本文介绍了斯威夫特 - 无法出列一种视图:带有标识符的 UICollectionElementKindCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试加载 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'

第一次抛出调用栈:

我的代码

@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 - 必须为标识符或连接故事板中的原型单元'*第一次抛出调用栈:

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:

必须为标识符或连接情节提要中的原型单元

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")

这篇关于斯威夫特 - 无法出列一种视图:带有标识符的 UICollectionElementKindCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆