尝试加载UICollectionView时收到此错误消息 .
2015-07-23 16:16:09.754 XXXXX [24780:465607]由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使类型的视图出列:具有标识符CollectionViewCell的UICollectionElementKindCell - 必须注册一个nib或类用于标识符或连接故事板中的原型单元格'第一个抛出调用堆栈:
我的代码
@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
}
我已经在storyboard检查器中声明了 CollectionViewCell
,但仍然出现错误消息 .
3 回答
看了你的例外后:
最后一部分是最重要的:
这意味着您的集合视图尚未注册您的自定义单元格 . 要解决此问题,请在
viewDidLoad
中添加以下内容:对于Swift 3:
在你的viewDidLoad()中放入此代码