unable to dequeue a cell with identifier cell with properly defined identifier - ios

I experienced weird issue with TableViewController
*** Assertion failure in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3694.4.18/UITableView.m:7732
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
It makes no sense while
Identifier is set properly.
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
However if i place
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
in ViewDidLoad, then it works. However prototype from storyboard will not and is not used.
At this point i have no idea what's going on.
TableViewController class is set also correctly.

I understood your question, There are only one reason that your tableview can not find the registered cell.
Solution is that you have to check your code one more time. Please check your identifier spelling in both storyboard and cellForRowAt method.
if this solution not work than follow this steps:
paste below code in cellForRowAt method
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
Delete all cells from tableviewcontroller from storyboard.
Drag and drop new TableViewCell and give proper indetifer "cell"
Build and run.
This solution will definitely work.

As you using a TableViewCell Differently So I am sure you had also created a tableCell Class , as in my case its "CustomTableCellClass" as follow
class CustomTableCellClass: UITableViewCell {
weak var CategoriesDelegate: CategoriesVcDelegate?
weak var itemsDelegate: ItemsvcDelegate?
//MARK: Outlets of CategoriesVcTableView
#IBOutlet weak var ProductImage: UIImageView!
#IBOutlet weak var ProductName: UILabel!
#IBOutlet weak var ProductPrice: UILabel!
#IBOutlet weak var ProductStarImage: UIButton!
}
My cellReuseIdentifier Declaration -
let cellReuseIdentifier = "cell"
Now Deque it as
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.CategoriesTableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! CustomTableCellClass
cell.CategoriesDelegate = self
cell.ProductStarImage.tag = indexPath.row
cell.ProductName.text = productNameArray[indexPath.row]
cell.ProductPrice.text = productPriceArray[indexPath.row]
cell.ProductImage.image = productImagesArray[indexPath.row]
return cell
}
Hope it helps :)

It seems that issue was in custom library i was using for navigation. Instead initialising view controller from storyboard identifier, it was initialising class directly, thus storyboard view controller was useless.
Everything works well now.

Related

implicitly unwrapping an Optional value on custom cell of table view

first of all i'm saying straight forward I know this is "duplicate" and and this is my 2nd time asking the same question - the problem is that my first one has been closed without me understanding the problem so please if someone wants to close this question again first let me understand what am I doing wrong. The solution I got last time was not relevant so if I could get addressed specifically that would be great!
I am trying to create a custom cell on tableview from an array. when I append any filed on my custom cell I get unexpected nil on all of the fileds and I have no idea why
this is my custom cell:
class CustomMovieCell: UITableViewCell {
#IBOutlet weak var title: UILabel!
#IBOutlet weak var rating: UILabel!
#IBOutlet weak var releaseYear: UILabel!
#IBOutlet weak var genre: UILabel!
var imageBackground: String!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
and this is my UITableView cellForRowAtIndexPath method:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! CustomMovieCell
let movieFetched = Movie(title: moviesArray[indexPath.row].title, image: moviesArray[indexPath.row].image, rating: moviesArray[indexPath.row].rating, releaseYear: moviesArray[indexPath.row].releaseYear, genre: moviesArray[indexPath.row].genre)
print(movieFetched)
cell.title.text? = movieFetched.title
cell.rating.text? = String(movieFetched.rating)
cell.releaseYear.text? = String(movieFetched.releaseYear)
cell.genre.text? = String(movieFetched.genre[0])
return cell
}
what am I missing? when appending ANY of the files I get unexpectedly found nil while unwrapping optional value - I did not know UIlabel as IBOutlet are optional? even-though they are not optional in my custom cell class.
when debugging I can see that all values of the cell - title, image, rating, releaseYear and genre are nil when trying to assign them a value - so I really have no idea what to do at this point. I have deleted and re-created the cell from scratch and it did not make any differents.
As I already stated - I know this is "duplicate". please though - do not close it before you help me because I did not get any answer the last time, I got directed to a wall-of-text page that did not help me understand my issue. The other "duplicate" pages are like a general "what are optional values" kind of question and do not help me with this specific issue.
edit:
I have uploaded this project to github if it helps anyone help me to figure out this issue
https://github.com/alonsd/MoviesApi
You've connected the custom cell class with 2 cells. One is in xib and another one is in this UIViewController. This UIViewController's prototype cell doesn't have these labels. So it will be nil and it will crash
Delete the prototype cell from MoviesViewController in storyboard. And add this in MoviesViewController viewDidLoad
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "MovieCell")
Change tableView cellForRowAt method as follows
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MovieCell") as! TableViewCell
cell.title.text = moviesArray[indexPath.row].title
cell.rating.text = String(moviesArray[indexPath.row].rating)
cell.releaseYear.text = String(moviesArray[indexPath.row].releaseYear)
cell.genre.text = String(moviesArray[indexPath.row].genre[0])
return cell
}

Custom view cell always has properties set to nil (UITableView)

Table view cell in cellForRowAt alway has all properties set to nil
import UIKit
class TodoTableViewCell: UITableViewCell {
#IBOutlet weak var label: UILabel!
}
class TodosViewController: UITableViewController {
#IBOutlet var TodosTableView: UITableView!
var projects = [Project]()
var todos = [Todo]()
override func viewDidLoad() {
super.viewDidLoad()
TodosTableView.delegate = self
self.tableView.register(TodoTableViewCell.self, forCellReuseIdentifier: "TodoTableViewCell1")
// data init
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "TodoTableViewCell1"
var todo = projects[indexPath.section].todos[indexPath.row]
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TodoTableViewCell else {
fatalError("The dequeued cell is not an instance of MealTableViewCell.")
}
cell.label?.text = todo.text // cell.label is always nil
return cell
}
}
It seems like identical issue
Custom table view cell: IBOutlet label is nil
What I tried to do:
- restart Xcode
- recreate outlet
- clean project
- recreate view cell from scratch like here https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/
Please help, iOS development drives me nuts already.
You don't need to register the class in the tableview if you're using prototype cells in Interface Builder. Try removing the registration function from viewDidLoad. Incidentally you can also set dataSource and delegate in IB - much neater code-wise.
You are using the UITableView instance method:
func register(AnyClass?, forCellReuseIdentifier: String)
This only works if your custom UITableViewCell subclass is not setup using Interface Builder
If you've created your subclass using an xib. You should use:
func register(UINib?, forCellReuseIdentifier: String)
like:
let nib = UINib(nibName: "\(TodoTableViewCell.self)", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "TodoTableViewCell1")
If you're using prototype cells in a storyboard you don't need to register your cells at all.
I think the identifier of the cell should be in the identifier from the attributes inspector column not the Identity inspector
and in module in Identity inspector add your project
Important note: One issue I haven't seen discussed is that if you use prototype cells in the storyboard, then explicitly registering the cell will make your outlets nil! If you explicitly register the cell then you are registering it without the storyboard which has your iboutlets. This will mean you defined your outlets in your cell but they aren't connected. Deleting the explicit registration will solve the issue.
Doesn't work:
tableVIew.register(MenuCell.self, forCellReuseIdentifier: "MenuCell")
Works:
// tableVIew.register(MenuCell.self, forCellReuseIdentifier: "MenuCell")

Load UITableViewCell from xib file in a UITableView in a view

I have a view (not a view controller) that pop up when I press a button.
In this view, I have a UIButton and a UITableView. I want to use the UITableViewCell I designed in a xib file.
So I created my cell class:
class PickerViewCell: UITableViewCell {
#IBOutlet weak var flagImageView: UIImageView!
#IBOutlet weak var codeLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
func setCell(myObject: myObject) {
flagImageView.image = UIImage(named: myObject.code)
codeLabel.text = myObject.code
codeLabel.font = myObject.kPickerViewCodeFont
nameLabel.text = myObject.name
nameLabel.font = myObject.kPickerViewNameFont
}
}
My xib file:
Here I didn't set the owner of the xib (I tried with as well), I put the custom class for the UITableViewCell and no reuse identifier (I tried with as well).
In the init function of my view (the one containing the table view), I do the following:
let nib = UINib(nibName: "PickerViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
And finally I implement the method:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell
let myObject = myObjects[indexPath.row]
cell.setCell(myObject)
return cell
}
When I want to show (create) my view, the debugger stop at
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell
and I get the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f9b934d8c70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key codeLabel.'
What's wrong?
EDIT:
Check your connection of #IBOutlet weak var codeLabel: UILabel! with IB in PickerViewCell. Try to reconnect it.

can't set UITableViewCell as a cell

I have made a ViewController, in that ViewController, I have made a TableView , in the TableView I have added a TableViewCell.
In the TableViewCell, I have added a text label and an image.
Iv'e made a cocoa touch file named customCell and connected it with the TableView Cell.
In the ViewController file I wrote this:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
cell.imageCountry.image = UIImage(named: "2xd.png")
cell.nameCountry.text = "hey"
return cell
This is what i wrote in the customCell file:
import UIKit
class customCell: UITableViewCell {
#IBOutlet var imageCountry: UIImageView!
#IBOutlet var nameCountry: UILabel!
}
Iv'e connected the TableView & the ViewController with the DataSource & Delegate.
When I run my code this is the error I get:
Could not cast value of type 'UITableViewCell' (0x10d1119f0) to 'refreshing.customCell' (0x10b2f6dd0).
*refreshing is the name of the project.
And the green line of the bugger is set to this line:
let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell") as! customCell
Any suggestions?
you are using the old decking method, that either rquires you to create the cell or use the new that takes the indexPath
let cell:customCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! customCell
if it is still failing, you need to register the cell, either by setting it up in the storyboard or by registering either a class or a nib file in code.
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: "cell")
}
or
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(customCell.self, forCellReuseIdentifier: "cell")
}
You need to set your custom class as the cell's class here ^
And to subclass, ensure in your CustomClass.h file you have
#interface CustomClass : UITableViewCell

This class is not key value coding-compliant for the key...why?

I've linked output from the IB to the code, as shown below.
class DiaryTableViewCell: UITableViewCell {
#IBOutlet weak var TitleLabel: UILabel!
#IBOutlet weak var SubTitleLabel: UILabel!
#IBOutlet weak var leftImageView: UIImageView!
#IBOutlet weak var rightImageView: UIImageView!
}
Here, I'm registering the class:
override func viewDidLoad() {
self.title = "My Diary"
cellNib = UINib(nibName: "TableViewCells", bundle: nil)
tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
}
But I keep getting the following runtime error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '...setValue:forUndefinedKey:]: this class is not key value
coding-compliant for the key SubTitleLabel.'
From within the following code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?
if (cell == nil) {
tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
cell?.selectionStyle = .None
}
if (cell != nil) {
println("\(x++)) Inside cell")
cell!.TitleLabel.text = "Hello"
cell!.SubTitleLabel.text = "World"
}
return cell!
}
Specifically, it's happening here:
cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
Question: How am I violating the key value coding-compliant for a UILabel?
This hasn't happened before... UILabel is KVO compliant.
I linked to the WRONG Source!
Here's the result:
You should not be calling instantiateWithOwner yourself inside tableView:cellForRowAtIndexPath.
Register the nib in viewDidLoad and then dequeueReusableCellWithIdentifier will do all the work for you.
The reason for your particular error is that you are calling instantiateWithOwner passing self as the owner and so the nib is trying to wire the outlets up to your UITableViewDataSource implementation class rather than a DiaryTableViewCell.
Show the references of your ViewController rigth-clicking in it on the Document Outline. Probably you will see a warning in one of the references. Delete it and link it again if still need it.
sometimes its like when you create button in your xib, you create one button and copy paste other buttons from one buttons, in that case this error occurs, and yes removed connections from xib could also be an reason.
I have created a TableViewCell same like your & have the same problem. I have research solving the problem but nothing. Then I delete Label in TableViewCell and recreate again, connect it to TableViewCell through File Owner,v..v And the result is right. No error. You should recreate them again.

Resources