swift - 为什么我不能将 UICollectionViewCell 向下转换为它的子类?

标签 swift casting uicollectionview

我正在制作一个 Collection View ,并制作了我自己的自定义 collectionCell。我已经在身份检查器中指定了,据我所知,我做的一切都是正确的。

代码是

import UIKit

class SubjectCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
}

在我的 collectionViewController 中

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

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

    return cell
}

我收到以下错误消息

“无法将‘UICollectionViewCell’ (0x110460820) 类型的值转换为‘project.SubjectCollectionViewCell’ (0x10eceeda0)。 (lldb)”

最佳答案

引用:Could not cast value of type 'UICollectionViewCell'

如果您在 collectionViewController 中看到它,请将其删除。 self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

关于swift - 为什么我不能将 UICollectionViewCell 向下转换为它的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36942694/

相关文章:

c++ - 为什么 C 和 C++ 允许表达式 (int) + 4*5?

ios - 自定义 UICollectionViewCell 不显示 UIImage 和崩溃

swift - 我如何在聊天中显示用户最后一条消息旁边的个人资料图片

ios - 为什么我什至没有使用 Bolts 时会收到有关 Bolts 框架和 FacebookSDK 的错误?

ios - 从 "collapsed"单元格触发 segue

swift - UITableview 在 View 顶部添加空白

ios - AutoSizingCells preferredLayoutAttributesFitting 性能 [WWDC 2018]

ios - 在 ViewWillAppear 和 ViewWillDisappear UI 故障处隐藏/显示导航栏

java - 使用 <?> 参数调用通用 Java 函数

c++ - 为什么从 "int"到 "float"的类型转换会将我的值减半?