Getting Uitableview Error "Unable to Dequeue a Cell with Identifier Cell"

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

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?

Swift 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

Well, i'm not sure how you configured your search controller but if you have initialized the controller with another controller, you must register programmatically your cell every time the results VC appears on the screen.

init(searchResultsController searchResultsController: UIViewController?)

That is true if searchResultsController != nil

You can register the cell in the following method:

func willPresentSearchController(_ searchController: UISearchController)

How to fix 'unable to dequeue a cell' error in Swift

If you are getting an “unable to dequeue a cell” error even though each table has registered its appropriate cell identifier, that means that you likely instantiated a cell for one table when cellFor was called for the other. This is easy to do when you have a single set of UITableViewDataSource methods servicing both tables. You can confirm this by adding a breakpoint in cellFor, compare the tableView passed to that method to your two outlets for messageTableView and listTableView: Ensure sure you’re instantiating a cell appropriate for that table.

Rather than having cellFor rely upon currentTable, I’d suggest just checking tableView to determine which type of cell is to be instantiated. Then it’s impossible to instantiate the wrong cell at all. You can probably even eliminate currentTable altogether.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch tableView {
case messageTableView:
...

case listTableView:
...

default:
fatalError()
}
}

Frankly, I’d discourage using a single UITableViewDataSource for two different tables. An alternative is to encapsulate the UITableViewDataSource for each into its own object, and it again makes it impossible to instantiate the wrong cell for the given table. It also helps mitigate view controller bloat.

Unable to dequeue a cell with identifier costumeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

You probably need to register your cell. It would look something like this.

If you're using a cell class in code:

tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "Cell")

If you're using a cell with a nib:

let nib = UINib(nibName: "MyTableViewCellNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "Cell")

If you're using a storyboard you need to make sure your prototype cell is setup properly. This answer actually has that defined well https://stackoverflow.com/a/14939860/563381

https://developer.apple.com/reference/uikit/uitableview/1614937-register

Swift - UITableView 'unable to dequeue a cell with identifier Cell'

Make sure that somewhere (probably in TableViewController's viewDidLoad), you register the cell class:

Swift 4:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

Swift 3 and below:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")


Related Topics



Leave a reply



Submit