为什么我不能将UICollectionViewCell转发为其子类?

soe*_*606 5 casting uicollectionview swift

我正在制作一个集合视图,并制作了我自己的自定义collectionCell.我已在身份检查员中指明了这一点,据我所知,我做的一切都是正确的.

代码是

import UIKit

class SubjectCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
}
Run Code Online (Sandbox Code Playgroud)

并在我的collectionViewController中

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SubjectCollectionViewCell

    return cell
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息

"无法将'UICollectionViewCell'类型的值(0x110460820)转换为'project.SubjectCollectionViewCell'(0x10eceeda0).(lldb)"

小智 12

参考:无法转换'UICollectionViewCell'类型的值

如果你在collectionViewController中看到这个,删除它. self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)


gna*_*729 -2

您是否注册了reuseIdentifier,以便它将创建正确类的对象?

  • 这是一个问题,而不是一个答案。应该作为评论发布。 (2认同)