Reputation: 7778
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:
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 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
Reputation: 4277
myCollectionView!.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cellID")
is the code recommended. Worked for me. Hope it helps.
Upvotes: 3
Reputation: 368
If your are not using any custom class just use in ViewDidLoad
myCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
Upvotes: 3
Reputation: 408
Try this:
self.collectionView.registerClass(CollectionViewCell.self,forCellWithReuseIdentifier:"CELL")
Upvotes: 1
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
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