UICollectionView error using Swift

I get this error while 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'm registering a cell:

  • 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 } 

and cell class:

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

Any help appreciated

+7
ios objective-c swift uicollectionview uicollectionviewcell
source share
5 answers

You need to pass your subclass of Swift-style UICollectionViewCell to registerClass:

 self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL") 
+17
source share

If you are not using any custom class, just use in ViewDidLoad

 myCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
+3
source share
 myCollectionView!.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cellID") 

- recommended code. Worked for me. Hope this helps.

+3
source share

Try the following: self.collectionView.registerClass(CollectionViewCell.self,forCellWithReuseIdentifier:"CELL")

+1
source share

For your cell:

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

For your ViewController:

 import UIKit class NextViewController: UIViewController { @IBOutlet var collectionView : UICollectionView var ListArray=NSMutableArray() override func viewDidLoad() { super.viewDidLoad() var nipName=UINib(nibName: "GalleryCell", bundle:nil) collectionView.registerNib(nipName, forCellWithReuseIdentifier: "CELL") for i in 0..70 { ListArray .addObject("C: \(i)") } } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section:Int)->Int { return ListArray.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as GalleryCell cell.titleLabel.text="\(ListArray.objectAtIndex(indexPath.item))" return cell } func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize { return CGSizeMake(66, 58) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+1
source share

All Articles