ICL1901
ICL1901

Reputation: 7778

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

Upvotes: 6

Views: 15451

Answers (5)

ppalancica
ppalancica

Reputation: 4277

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

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

Upvotes: 3

Jibin Jose
Jibin Jose

Reputation: 368

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

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

Upvotes: 3

user2545330
user2545330

Reputation: 408

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

Upvotes: 1

PREMKUMAR
PREMKUMAR

Reputation: 8349

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

Upvotes: 1

Matt Gibson
Matt Gibson

Reputation: 38238

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

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

Upvotes: 17

Related Questions