我收到此错误,尝试在Swift中使用UICollectionView:

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

但是我想我正在注册该单元格:
  • ViewDidLoad:
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.collectionView.registerClass(NSClassFromString("CollectionCell"),forCellWithReuseIdentifier:"CELL")
    }
    
  • 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")
    

    10-08 00:56