Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView error using Swift

I am getting this error, trying to use a UICollectionView in Swift:

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

But I think I am registering the cell:

  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
    }
    

and the cell class:

    class CollectionCell: UICollectionViewCell
    {

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

        } 
     }

Any help appreciated

like image 464
ICL1901 Avatar asked Jun 12 '14 07:06

ICL1901


2 Answers

You need to pass your sub-class of UICollectionViewCell, in the Swift style, to registerClass:

self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL")
like image 141
Matt Gibson Avatar answered Oct 05 '22 16:10

Matt Gibson


If your are not using any custom class just use in ViewDidLoad

myCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
like image 43
Jibin Jose Avatar answered Oct 05 '22 16:10

Jibin Jose