6

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

5 Answers 5

17

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

self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Progress, Now I get an "use of unimplemented initializer 'init(frame:)", but I'm getting there!
That this solves.. init(frame: CGRect) { super.init(frame: frame) } Thank you again.
3

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

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

Comments

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

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

Comments

1

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

2 Comments

Thanks, but I prefer to stick with registerClass rather than registerNib.
But I'll keep this for next time.. +1
1

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

1 Comment

Thanks for answering.. Changing as you suggest results in the following console output: 0x1001bffeb: leaq 0x16067(%rip), %rax ; "Swift dynamic cast failed" 0x1001bfff2: movq %rax, 0x77c67(%rip) ; gCRAnnotations + 8 0x1001bfff9: int3 0x1001bfffa: nopw (%rax,%rax)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.