Downvote Me Downvote Me - 1 year ago 84
Swift Question

"unable to dequeue a cell with identifier" when switching to programmatic transition instead of segue

I have 2 views, a main view with a button inside, and a table view. The table view cell has the identifier "ResultsTableViewCell". My goal is to make it so that when I press the button on the main view, I transition to the table view

When I use a storyboard segue, everything works fine. However, when I take out the segue and instead assign the button an action to make the transition programmatically, I get the error mentioned. Why is this?

Here is the code:

ViewController.swift (this is the only file I'm making any changes in)

class ViewController: UIViewController {

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

/* How come this throws an unable to dequeue error but a segue works?
@IBAction func buttonMe(sender: UIButton) {
let a = ResultsTableViewController()
let b = self.navigationController
b!.pushViewController(a, animated: true)



import UIKit
class ResultsTableViewController: UITableViewController {

override func viewDidLoad() {
// I found out the next line overwrites any cell customization and commented it out, so this isn't the problem
//self.tableView.registerClass(ResultsTableViewCell.self, forCellReuseIdentifier: "ResultsTableViewCell")
self.navigationController!.navigationBar.hidden = false
self.navigationItem.title = "Select Property"
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ResultsTableViewController.cancelButton(_:)))

func cancelButton(sender: UIBarButtonItem){
//dismissViewControllerAnimated(true, completion: nil)

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ResultsTableViewCell", forIndexPath: indexPath) as! ResultsTableViewCell
cell.label1.text = "sUP"
cell.label2.text = " heLLo"
return cell

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//sender.selectedResult = propertyList[(indexPath as NSIndexPath).row]
// Dismiss this view and map the location
self.dismissViewControllerAnimated(true, completion: nil)

// 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?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.



import UIKit

class ResultsTableViewCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

override func awakeFromNib() {
// Initialization code

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state


If you're wondering why I don't just use a segue, there are 2 reasons:

  1. I want to learn why this occurs

  2. My real app transitions on a special case that I need to program in

I'm also dead certain that I set the cell identifier in storyboard. The segue wouldn't work otherwise. Lastly, both views are embedded in nav controllers already

Answer Source

You aren't creating the viewController correctly. When you do this:

let a = ResultsTableViewController()

You get an instance of the class ResultsTableViewController(), but you don't get anything that was laid out in the Storyboard. There is no connection from the code to the Storyboard.

Instead, you need to ask the Storyboard to instantiate the viewController:

if let a = self.storyboard?.instantiateViewControllerWithIdentifier("resultsTable") as? ResultsTableViewController {
    // push a

In order for this to work, the Storyboard ID needs to be set in the Identity Inspector.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download