I got this error message when trying to load UICollectionView.
2015-07-23 16:16:09.754 XXXXX[24780:465607] Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason: 'could
not dequeue a view of kind: UICollectionElementKindCell with
identifier CollectionViewCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'
First throw call stack:
My code
#IBOutlet var collectionView: UICollectionView!
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "category")
return cell
}
I already declared CollectionViewCell in storyboard inspector but the error message still occur.
After taking a look at your exception:
2015-07-23 16:16:09.754 XXXXX[24780:465607] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason: 'could
not dequeue a view of kind: UICollectionElementKindCell with
identifier CollectionViewCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard' * First throw
call stack:
last part is most important:
must register a nib or a class for the
identifier or connect a prototype cell in a storyboard
Which means that your collection view doesn't have registered your custom cell. To resolve this add following in your viewDidLoad:
var nib = UINib(nibName: "UICollectionElementKindCell", bundle:nil)
self.collectionView.registerNib(nib, forCellReuseIdentifier: "CollectionViewCell")
For Swift 3:
collectionView.register(YourCustomCellClass.self, forCellWithReuseIdentifier: "cell")
In your viewDidLoad() put in this code
collectionView.registerClass(YourCustomCellClass.self, forCellWithReuseIdentifier: "cell")
Related
Searched here and there but I couldn't find anything that I haven't already tried to fix my issue, so going to ask it again.
I am trying to use a collection view with custom cells.
The main collection view is in the Main View Controller.
I'm using a generic cell named GenericHomeCollectionViewCell, then customize another collection view that it has. This cell doesn't have any issue with registering and dequeuing. (It might sound weird but I already have an application with this usage and it works without any problem, so I wanted to use the same method again.)
The crash happens when I try to register and dequeue cells inside that generic cell.
I have 4 different custom cells for use which are registered in awakeFromNib().
GenericHomeCollectionViewCell.swift
override func awakeFromNib() {
super.awakeFromNib()
newsCollectionView.register(UINib(nibName: "HeadlineNewsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HeadlineNewsCollectionViewCell")
newsCollectionView.register(UINib(nibName: "HomeAuthorCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeAuthorCollectionViewCell")
newsCollectionView.register(UINib(nibName: "NewsListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "NewsListCollectionViewCell")
newsCollectionView.register(UINib(nibName: "HomeDetailedNewCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeDetailedNewCollectionViewCell")
newsCollectionView.delegate = self
newsCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if sectionType == .swipe {
let cell : HeadlineNewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineNewsCollectionViewCell", for: indexPath) as! HeadlineNewsCollectionViewCell
cell.prepareCell(news: newsList[indexPath.row])
return cell
} else if sectionType == .homeAuthor {
let cell : HomeAuthorCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeAuthorCollectionViewCell", for: indexPath) as! HomeAuthorCollectionViewCell
cell.prepareCell(news: newsList[indexPath.row])
return cell
} else {
let cell : HeadlineNewsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineNewsCollectionViewCell", for: indexPath) as! HeadlineNewsCollectionViewCell
cell.prepareCell(news: newsList[indexPath.row])
return cell
}
}
When I launch the application, I get crash with error:
*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], UICollectionView.m:6502
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier HeadlineNewsCollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I tried to give a reuse identifier name from the storyboard editor's but there is no difference.
I have a Viewcontroller scene (Storyboard) with a UITableView and inside a UITableViewCell called Foo. Foo loads fine when loaded from a storyboard, but I need to load it from another UITableView programmatically.
I'm registering the cell like this (at first I tried without UINib but IBOutlets were nil):
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "Foo", bundle: nil), forCellReuseIdentifier: "bar")
}
and then loading it like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bar", for: indexPath) as! Foo
return cell
}
This doesn't work and returns the error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <[path]> (loaded)' with name 'Foo''
Things I made sure of:
The Foo UITableViewCell identifier in Attributes inspector is Foo
Has the correct Target Membership
You may want to create a separated nib file for your cell then register it to your table view like what you've done above.
I'm building a Keyboard extension in Swift3.
First time my keyboard is launch, no issue, my collection view is displayed juste fine, everything works.
This bug occurs when my keyboard goes to background and go back again in foreground. For example, I launch iMessage, display my keyboard, tap on the "home button" of my iPhone, launch iMessage again -> crash:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier KeyboardViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Here is how I set my storyboard for my cell identifier:
With the right class:
And here is how I call it from the cellForItemAt indexPath method:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! KeyboardViewCell
return cell
I don't understand why this crash after changing keyboards, like there is no storyboard loaded in that case or something...Did I forgot something?
My storyboard is like this:
Your KeyboardViewController holds two UICollectionViews. Make sure MyCollectionView is calling its dataSource when dequeueing the KeyboardViewCell in cellForItemAt indexPath:
if collectionView === myCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! KeyboardViewCell
return cell
} else {
//dequeue soundCollectionViewCell
}
If you are conforming to the same protocol for more than one "delegator" (i.e. more than one ColletionView), you have to implement this check in every callback method.
I'm actually doing an app which loads contents to a UICollectionView as the user selects 1 it should create a UITableViewCell and add it to an UITableView on the right but I keep getting errors
In the ViewDidLoad I register the Nib (where cart #IBOutlet var cart: UITableView? and is connected to Storyboard)
cart!.registerNib(UINib(nibName: "cartCell", bundle: nil), forCellReuseIdentifier: "Cell")
When the cell is selected I add the object to an array and refresh the table
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.row < serviceList.count {
let service:Service = serviceList[indexPath.row]
cartOrder.append(service)
dispatch_async(dispatch_get_main_queue()){
self.cart!.reloadData()
}
}
}
But when the table is filling its content I get an error at the declaration of cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! cartCell
let serv = cartOrder[indexPath.row]
cell.lblName.text = serv.name
cell.lblDescription.text = serv.description
cell.imgService.image = UIImage(named: "star")
return cell
}
The error Im getting is
2016-02-19 16:41:33.375 trackMyDevice_1.0[43188:5851609] ***
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<NSObject 0x7fe645009320> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key imgService.'
Any help is well appreciated
I register the Nib
So you have a nib called cartCell.xib, is that right? And that's what you are registering here:
cart!.registerNib(UINib(nibName: "cartCell", bundle: nil),
forCellReuseIdentifier: "Cell")
Later you load that nib to get a cell, by saying this:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",
forIndexPath: indexPath) as! cartCell
Now, that line makes an explicit claim that the cell loaded from the nib will be a cartCell. But the problem is that the top-level object in that nib, cartCell.xib, is in fact not a cartCell. It isn't any kind of UITableViewCell. It is an NSObject! And thus, you crash with a complaint that this thing is an NSObject:
<NSObject 0x7fe645009320> ... this class is not key value coding-compliant for the key imgService
I am getting an error Thread1: signal SIGABRT in the line below, I already tried:
Product>Clean, Restart Computer
check all connections to the storyboard
check if some connection is lost
the Identifier is set to Cell in the attribute inspector
plus look into many posts in this forum.
After all this I deleted all connections with storyboard and connect again but still get the same error :(
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell2 = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! DefinitionTableViewCell
In the console:
Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-3347.44/UITableView.m:6245
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell2 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Any help is more than welcome Tks :)
Well, as the exception message suggests, you "must register a nib or a class for the identifier". Try adding this line in viewDidLoad():
tableView.registerNib(UINib(nibName: "nameOfYourCustomNib", bundle: nil), forCellReuseIdentifier: "cell2")
Replace the nameOfYourCustomNib with your nib name, for example if you have a file MyCustomCell.xib then the name would be MyCustomCell.
If this fails, try registering a class instead of a nib (remember to delete the first line), by typing:
tableView.registerClass(DefinitionTableViewCell.self, forCellReuseIdentifier: "cell2")
And the most important thing, in cellForRowAtIndexPath, make sure you check if the cell is nil, and if so, create a new DefinitionTableViewCell.