加载UICollectionView时崩溃,为什么?

时间:2014-08-06 12:16:50

标签: ios swift uicollectionview uicollectionviewcell uicollectionreusableview

这就是我所看到的错误日志。

我有一个动态原型UICollectionViewCell,并在这里加载:

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    var cell: LeftMenuCollectionViewCell

cell = collectionView.dequeueReusableCellWithReuseIdentifier("xxx", forIndexPath: indexPath) as LeftMenuCollectionViewCell // <- at this line something goes wrong

    return cell
}

我之前正在注册课程:

self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "xxx")

现在使用iOS 8 beta 5

enter image description here

3 个答案:

答案 0 :(得分:2)

您已注册UICollectionViewCell而非您自己的手机类LeftMenuCollectionViewCell

的铸造
UICollectionviewCell as LeftMenuCollectionViewCell 

正在破坏并导致崩溃。要修复此寄存器正确的类

self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "xxx")

答案 1 :(得分:2)

同意下面的两个解决方案,我注册了错误的单元格,但之后我发现了另一个问题cell's outlets become nil。如果通过Interface Builder创建了UICollectionViewController,那么解决方案就是删除整个注册

答案 2 :(得分:1)

您已错误地注册了单元格。试试这个:

self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "xxx")
相关问题