ios - 使用 Swift 的 UICollectionView 错误

标签 ios objective-c uicollectionview swift uicollectionviewcell

我收到这个错误,试图在 Swift 中使用 UICollectionView:

NSInternalInconsistencyException', reason: 'attempt to register a cell class which is not a subclass of UICollectionViewCell ((null))

但我想我正在注册单元格:

  1. ViewDidLoad:

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.collectionView.registerClass(NSClassFromString("CollectionCell"),forCellWithReuseIdentifier:"CELL")
    }
    
  2. cellForItemAtIndexPath:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
    {
       var  cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as CollectionCell
    
        cell.titleLabel.text="cellText"
        return cell
    }
    

和细胞类:

    class CollectionCell: UICollectionViewCell
    {

        @IBOutlet var titleLabel : UILabel
        init(coder aDecoder: NSCoder!)
        {
            super.init(coder: aDecoder)

        } 
     }

感谢任何帮助

最佳答案

您需要以 Swift 样式将 UICollectionViewCell 的子类传递给 registerClass:

self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL")

关于ios - 使用 Swift 的 UICollectionView 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24179236/

相关文章:

ios - UICollectionView Scroll 无法正确分页

ios - UICollectionViewTransitionLayout - 模仿 iOS7 照片 collectionview 布局

ios - 如何将表输出传递到另一个 View Controller ?

ios - 如果我使用仅自 iOS 10 起可用的内容,这是否意味着我无法在 iOS 9 上安装该应用程序?

objective-c - PDF 中的上下文菜单

iphone - 检查绘制的路径/CGPath 是否在 iPhone 游戏中与自身相交

ios - 如何检测 AVAudioRecorder 是否暂停?

iphone - 更改选项卡栏 Controller 中选项卡的 View Controller

ios - 加速计在第一款游戏中有效,但在第二款游戏中变得疯狂

ios - 为什么我的 UICollectionView 在单击单元格时会向上滚动?