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

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")

Related

How do I add custom cells for a tableView which has been added on top of a UIViewController?

I have used a tableView (myTableView) on a usual UIViewController class. I wish to use reusable cells in this tableView to save memory. I did not create separate XIB and dragged and dropped a tableView component on to the viewController.
The cells have been created with a new class HistoryTableViewCell of type UITableViewCell and this class also has an XIB.
I have also created a tableViewCell and its XIB and used the following code in the tableView(_,cellForRowAt:) method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "historyReuseIdentifier", for: indexPath) as! HistoryTableViewCell
cell.meetingDateLabel.text = historyArray![indexPath.section]
return cell
}
This line of code works fine with a tableViewController when I add the following line in the viewDidLoad() method:
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
But the tableView.register property cannot be used with a tableView. How do I make use of the reusable cells here?
Lets say your xib is ok, everything works fine and the only thing left is to register the nib.
First declare your table view:
#IBOutlet weak var tableView: UITableView!
In the viewDidLoad() :
tableView.register(UINib(nibName: "HistoryTableViewCell", bundle: nil) , forCellReuseIdentifier: "historyReuseIdentifier")
Also check your tableView's dataSource and delegate.
The answer was actually quite simple. I figured it out after reading a few of the other answers.
All I needed was to replace this line:
tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
with this one:
UITableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
Since there is no table view by the name tableView, you need to access the UITableView class.
Do you want to reuse the cells thats ok, to use them you need to register it first..suppose you have multiple tableView in a single ViewController, then you have to register each cell with respective tableView.
just register the tableViewCell with respective tableView and use its reusableIdentifier which you have used while registering the tabelViewcell.
Try to create it directly in cellForRowAt indexPath: like this. Here no need of tableView.register(UINib(nibName: "TableViewCell", ... line.
//And replace names with your names
var cell = tableView.dequeueReusableCell(withIdentifier:"cell") as? TableViewCell
if cell == nil {
cell = Bundle.main.loadNibNamed("TableViewCell",owner: self, options: nil)?.first as? TableViewCell
}

Register UITableViewCell not working

I am trying to register UITableViewCell in viewdidload
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
In cellForRowAtIndex
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.productNameLabel.text = "Product"
cell.productNameLabel.textColor = UIColor.darkGray
return cell
}
Here it is crashing in cell.productNameLabel.text.
What is the purpose of registering cell? why it is crashing?
I want to reload data even if cell or table is not visible.
Crashreport :
See the Apple's comments which answers your query on the purpose of registering cell :
Prior to dequeueing any cells, call this method or the
register(_:forCellReuseIdentifier:) method to tell the table view how
to create new cells. If a cell of the specified type is not currently
in a reuse queue, the table view uses the provided information to
create a new cell object automatically.
This is the standard procedure I apply while working with Custom Cells (if you are using xib) :
Set cell's identifier in Xib's attribute inspector :
Register Xib :
self.tableTasks.register(UINib(nibName: "TaskCell", bundle: nil), forCellReuseIdentifier: "taskCell")
However, if you are not using Xib and creating custom cell using code only, then use registeCell :
self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
Are you using a xib for this cell? If so, none of the outlets will be connected if you just register the class of the cell. You need to register the actual xib file, so that everything can be connected correctly when the cell is created. Have a look at
-(void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
https://developer.apple.com/documentation/uikit/uitableview/1614937-registernib
My method for register cell.
Syntax sugar
protocol BSCellProtocol {
// For `registerCell`
static var NibName: String! { get }
// For `registerCell`, `dequeueCellWithType`, and `dequeueHeaderFooterWithType`
static var Identifier: String! { get }
}
extension UITableView {
func registerCell(_ type: BSCellProtocol.Type) {
let nib = UINib(nibName: type.NibName, bundle: nil)
let identifier = type.Identifier!
self.register(nib, forCellReuseIdentifier: identifier)
}
func dequeueCellWithType<T: BSCellProtocol>(_ type: T.Type) -> T {
let cell = self.dequeueReusableCell(withIdentifier: type.Identifier) as! T
return cell
}
func dequeueCellWithType<T: BSCellProtocol>(_ type: T.Type, index: IndexPath) -> T {
let cell = self.dequeueReusableCell(withIdentifier: type.Identifier, for: index) as! T
return cell
}
}
Usage
class MyCustomCell: UITableViewCell, BSCellProtocol {
static var NibName: String! = "MyCustomCell"
static var Identifier: String! = "cellIdentifier_at_Xib"
#IBOutlet weak var lblTitle: UILabel!
// other IBOutlet components
}
// In ViewController, register cell
tableView.registerCell(MyCustomCell.self)
// dequeue cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// cell is `MyCustomCell` instance
let cell = tableView.dequeueCellWithType(MyCustomCell.self)
// configure cell ...
// ....
return cell
}
I had the same problem. I also was not using XIB for cell. My view was not connected to View in File's Owner Outlets. Maybe this info will help someone.

TableView CellReuse

I have problems with my custom cell file in tableview. I managed to get it done using the out comment line shown below, but the performance was really bad when it had 10+ cells.
UsingdequeueReusableCell leads to this error:
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier DiveNewsShort - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
which is strange, because I do register the nib in viewDidLoad(). I hope you can help me, I am getting frustrated by this.
class ProfilTableView: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "DiveNewsShort", bundle: nil), forCellReuseIdentifier: "DiveNewsShort")
tableView.register(DiveNewsShort.self, forCellReuseIdentifier: "DiveNewsShort")
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = Bundle.main.loadNibNamed("DiveNewsShort", owner: self, options: nil)?.first as! DiveNewsShort
// This one works as expected
let cell = tableView.dequeueReusableCell(withIdentifier: "DiveNewsShort", for: indexPath) as! DiveNewsShort
// This one does not
return cell }
Update:
I managed to get rid of the error by adding the register function in the cellForRowAt function, but I don't think that this is a efficient way actually. It should work within the vieDidLoad shouldn't it?
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UINib(nibName: "DiveNewsShort", bundle: nil), forCellReuseIdentifier: "DiveNewsShort")
let cell = tableView.dequeueReusableCell(withIdentifier: "DiveNewsShort", for: indexPath) as! DiveNewsShort
return cell }
You don't need this line:
tableView.register(DiveNewsShort.self, forCellReuseIdentifier: "DiveNewsShort")
You already have registered the nib file one line before.
There are three ways to register cells for reuse/dequeuing:
You are programmatically creating the cells, in which case you register the class in viewDidLoad.
You are using a NIB, in which case you register the NIB in viewDidLoad.
You are using storyboard cell prototypes, in which case you don't have to register anything. The storyboard does all of this for you.
Since you are using NIBs, you should remove the registering of the class and only register the NIB. And you should do this in viewDidLoad. This process is outlined in https://stackoverflow.com/a/28490468/1271826 as well as in Reinier's answer.
Looking at your MCVE, your problem was a result of a more fundamental mistake, where you had a UIViewController trying to use another view controller, which was a UITableViewController, to manage the table. But UITableViewController has its own UITableView and won't use the one that you have an #IBOutlet for, so you were registering the NIB for a table view you weren't seeing. There were a ton of other issues here (e.g. if you really want a view controller within a view controller, you have to do view controller containment calls, etc.), but the simplest solution was to excise this separate UITableViewController from the project and when this was fixed, it works precisely as we described. See https://github.com/robertmryan/Divers for a working version of your MCVE.
You also didn't hook up the outlets for the other two controls in your cell (the switch and slider). Thus, if you changed either of those two controls and then scrolled, the cells are reused and you see the changed UIKit control that was done for some other cell, but was subsequently reused. To fix that, your custom UITableViewCell subclass should have outlets for all controls, and cellForRowAt must set values for all of these outlets. You also need some mechanism for the cell to inform the view controller when the switch and slider have changed and update the model accordingly, so when cellForRowAt was later called for that row, it would know the state of that CellData to set the control appropriately. A common solution for this is to use the protocol-delegate pattern. See the above GitHub repo, which illustrates this pattern, too.
I have build this protocol for help me in this process
protocol CBNibInstanceableCellProtocol {
static func getCellXib() -> UINib?
static func getReuseIdentifier() ->String
}
and in your class you have to implement those methods like here
//example implementation
extension CBUsersAttendanceEmptyCell : CBNibInstanceableCellProtocol
{
static func getCellXib() -> UINib?
{
if Bundle.main.path(forResource: "CBUsersAttendanceEmptyCell", ofType: "nib") != nil
{
return UINib(nibName: "CBUsersAttendanceEmptyCell", bundle: nil)
}
return nil
}
static func getReuseIdentifier() ->String
{
return "CBUsersAttendanceEmptyCell"
}
}
then in your viewDidLoad you must do something like this
//example code
self.collectionView.register(CBUsersAttendanceAvatarCell.getCellXib(), forCellWithReuseIdentifier: CBUsersAttendanceAvatarCell.getReuseIdentifier())
self.collectionView.register(CBUsersAttendanceCountCell.getCellXib(), forCellWithReuseIdentifier: CBUsersAttendanceCountCell.getReuseIdentifier())
self.collectionView.register(CBUsersAttendanceEmptyCell.getCellXib(), forCellWithReuseIdentifier: CBUsersAttendanceEmptyCell.getReuseIdentifier())
in your cellForRow
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CBUsersAttendanceCountCell.getReuseIdentifier(), for: indexPath) as? CBUsersAttendanceCountCell
{
return cell
}
You must have defined the class for you view in your xib, this is very important check this pictures
Hope this helps

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

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

My UITableViewController is causing a crash with the following error message:
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'
I understand that I need to register a nib or a class but I don't understand 'where or how?'.
import UIKit
class NotesListViewController: UITableViewController {
#IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "preferredContentSizeChanged:",
name: UIContentSizeCategoryDidChangeNotification,
object: nil)
// Side Menu
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// whenever this view controller appears, reload the table. This allows it to reflect any changes
// made whilst editing notes
tableView.reloadData()
}
func preferredContentSizeChanged(notification: NSNotification) {
tableView.reloadData()
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
let note = notes[indexPath.row]
let font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
let textColor = UIColor(red: 0.175, green: 0.458, blue: 0.831, alpha: 1)
let attributes = [
NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSTextEffectAttributeName : NSTextEffectLetterpressStyle
]
let attributedString = NSAttributedString(string: note.title, attributes: attributes)
cell.textLabel?.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.textLabel?.attributedText = attributedString
return cell
}
let label: UILabel = {
let temporaryLabel = UILabel(frame: CGRect(x: 0, y: 0, width: Int.max, height: Int.max))
temporaryLabel.text = "test"
return temporaryLabel
}()
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
label.sizeToFit()
return label.frame.height * 1.7
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
notes.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
}
}
// #pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if let editorVC = segue.destinationViewController as? NoteEditorViewController {
if "CellSelected" == segue.identifier {
if let path = tableView.indexPathForSelectedRow() {
editorVC.note = notes[path.row]
}
} else if "AddNewNote" == segue.identifier {
let note = Note(text: " ")
editorVC.note = note
notes.append(note)
}
}
}
}
You can register a class for your UITableViewCell like this:
With Swift 3+:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
With Swift 2.2:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
Make sure same identifier "cell" is also copied at your storyboard's UITableViewCell.
"self" is for getting the class use the class name followed by .self.
Have you set the Table Cell identifier to "Cell" in your storyboard?
Or have you set the class for the UITableViewController to your class in that scene?
This worked for me, May help you too :
Swift 4+ :
self.tableView.register(UITableViewCell.self, forCellWithReuseIdentifier: "cell")
Swift 3 :
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
Swift 2.2 :
self.tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
We have to Set Identifier property to Table View Cell as per below image,
I had this issue today which was solved by selecting Product -> Clean. I was so confused since my code was proper. The problem started from using command-Z too many times :)
y my case i solved this by named it in the "Identifier" property of Table View Cell:
Don't forgot: to declare in your Class: UITableViewDataSource
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
Just drag a cell (as you did for TableViewController) and add in to it just by releasing the cell on TableViewController. Click on the cell and.Go to its attributes inspector and set its identifier as "Cell".Hope it works.
Don't forget you want Identifier on the Attributes Inspector.
(NOT the "Restoration ID" on the "Identity Inspector" !)
Match the identifier name at both places
This error occurs when the identifier name of the Tablecell is different in the Swift file and in the Storyboard.
For example, the identifier is placecellIdentifier in my case.
1) The Swift File
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "placecellIdentifier", for: indexPath)
// Your code
return cell
}
2) The Storyboard
One more reason for this issue to happen is an earlier problem. When showing a new ViewController, instantiating the target ViewController directly will of course not load the prototype cells from the StoryBoard. The correct solution should always be to instantiate the view controller through the story board like this:
storyboard?.instantiateViewController(withIdentifier: "some_identifier")
In Swift 3.0, register a class for your UITableViewCell like this :
tableView.register(UINib(nibName: "YourCellXibName", bundle: nil), forCellReuseIdentifier: "Cell")
I had the same problem. This issue worked for me. In storyboard select your table view and change it from static cells into dynamic cells.
My problem was I was registering table view cell inside dispatch queue asynchronously. If you have registered table view source and delegate reference in storyboard then dispatch queue would delay the registration of cell as name suggests it will happen asynchronously and your table view is looking for the cells.
DispatchQueue.main.async {
self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
self.tableView.reloadData()
}
Either you shouldn't use dispatch queue for registration OR do this:
DispatchQueue.main.async {
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.register(CampaignTableViewCell.self, forCellReuseIdentifier: CampaignTableViewCell.identifier())
self.tableView.reloadData()
}
There is two way you can define cell. If your table cell is inside on your ViewControllern then get the cell this way:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
// write your code here
return cell
}
But if you define cell outside of your ViewController then call the sell this way:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
// write your code here
return cell
}
And as everyone said don't forget to set your cell identifier:
Stupid mistake:
make sure you add register(TableViewCell.self, forCellReuseIdentifier: "Cell") instead of register(TableViewCell.self, forHeaderFooterViewReuseIdentifier: "Cell")
If you defined your cell through the Interface Builder, by placing a cell inside your UICollectionView, or UITableView :
Make sure you binded the cell with an actual class you created, and very important, that you checked "Inherit module from target"
It used to work on swift 3 and swift 4 but now its not working.
like
self.tableView.register(MyTestTableViewCell.self, forCellReuseIdentifier: "cell")
So I have tried the most of the solutions mentioned above in swift 5 but did not get any luck.
Finally I tried this solution and it worked for me.
override func viewDidLoad()
{
tableView.register(UINib.init(nibName: "MyTestTableViewCell", bundle: nil), forCellReuseIdentifier: "myTestTableViewCell")
}
I just met the same issue and see this post. For me it's because I forgot the set the identifier of cell, also as mentioned in other answers. What I want to say is that if you are using the storyboard to load custom cell we don't need to register the table view cell in code, which can cause other problems.
See this post for detail:
Custom table view cell: IBOutlet label is nil
Swift 5
you need to use UINib method to register cell in viewDidLoad
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
//register table view cell
tableView.register(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomTableViewCell")
}
I had the same issue where I registered my custom UITableViewCell classes within the viewDidLoad() which threw this error. To fix it what I did was registered the cells within the didSet property observer, as shown below
#IBOutlet tableview : UITableView! {
didSet {
tableview.register(CustomCell.self, forCellReuseIdentifier: "Cell")
}
}
Just for those new to iOS buddies (like me) who decided to have multiple cells and in a different xib file, the solution is not to have identifier but to do this:
let cell = Bundle.main.loadNibNamed("newsDetails", owner: self, options: nil)?.first as! newsDetailsTableViewCell
here newsDetails is xib file name.
I ran into this message when UITableView in the IB was moved into another subview with Cmd-C - Cmd-V.
All identifiers, delegate methods, links in the IB etc. stay intact, but exception is raised at the runtime.
The only solution is to clear all inks, related to tableview in the IB (outlet, datasource, delegate) and make them again.
If anyone is doing Unit Testing on a tableView and you're wondering why this error is appearing, just make sure that if you're using a text fixture, you must declare the system under test (SUT) in the setUp function correctly otherwise this error will keep coming up. It is also crucial you call loadViewIfNeeded() so the outlets between your code and storyboard are connected.
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
sutSearch = storyboard.instantiateViewController(identifier:String(describing: SearchTableViewController.self))
sutSearch.loadViewIfNeeded() // To make sure your outlets are connected.
}
In the “Subclass of” field, select UITableViewController.
The class title changes to xxxxTableViewController. Leave that as is.
Make sure the “Also create XIB file” option is selected.
Make sure you have the identifier in the attributes filled out with your cell identifier
I was also struggling with the same problem. I had actually deleted the class and rebuilt it. Someone, the storyboard had dropped the link between prototype cell and the identifier.
I deleted the identifier name and re-typed the identifier name again.
It worked.
If the classic solutions (register identifier for class in code or IB) do not work: try to relaunch Xcode, turns out my storyboard stopped saving edits I was made, including setting the reuse identifier.
My dynamic tableview was working properly, with cell identifier set on the Storyboard and in dequeueReusableCell(withIdentifier:.
I then switched the UITableView content from Dynamic Prototypes to Static Cells.
Running the app immediately caused the error, although the cell's identifier was still set to the same value on the Storyboard.
For a static table view, you must register the cell identifier outside the Storyboard:
tableView.register(EntryNutritionCell.self, forCellReuseIdentifier: "Cell")
or, comment out or remove cellForRowAtIndexPath: entirely. This function isn't really used by the Static table view, but is still called(?) and causes the crash:
// override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// return cell
// }
'Table View Cell' identifier must match the class identifier.
ex: if your 'Table View Cell' identifier is named "myCellId", then your code should be:
let myCell = tableView.dequeueReusableCell(withIdentifier: "myCellId", for: indexPath).
Also, after hours of troubleshooting i realized that having a GestureRecognizer class in my didLoad() was not allowing me to click table cells. so removing all 'hide keyboard' functionality from didLoad() and other extra code solved it for me.
I was struggling with the same problem. i have already check my reusableCell Identifier it was same as in my code. I deleted line of my code
"let Cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath)"
clean build
and write it again!
It worked.

Resources