UICollectionView error using Swift

15,335

Solution 1

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

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

Solution 2

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

myCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")

Solution 3

myCollectionView!.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cellID")

is the code recommended. Worked for me. Hope it helps.

Solution 4

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

Solution 5

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.
}
}
Share:
15,335
ICL1901
Author by

ICL1901

OLD programmer - from the 60s (1960s at least), stumbling aimlessly through xCode, Obj-C, PHP, mySQL, sqLite, (x)html, css, js, bbc, COBOL, exoplanets, slashdot, Apple. #SOreadytohelp

Updated on June 29, 2022

Comments

  • ICL1901
    ICL1901 almost 2 years

    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