18

I have been following the apple tutorial here and have come across an error:

2016-01-12 09:34:32.909 FoodTracker[1812:124509] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier MealTableViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

The error appears when the program is run, and the red highlighted line appears on the class line of AppDelegate.swift

These are the lines of code I believe are causing the error, as I found out through breakpoints:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "MealTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealTableViewCell

    // Configure the cell...
    let meal = meals[indexPath.row]

    cell.nameLabel.text = meal.name
    cell.photoImageView.image = meal.photo
    cell.ratingControl.rating = meal.rating


    return cell

}

I have looked around online, and a lot of answers have said to ensure that the TableCell has an identifier, however mine does and the error still pops up.

Please let me know if I need to post any more info.

Thanks in advance

7
  • have you registered your cell either via the interface builder or via code? Commented Jan 11, 2016 at 20:54
  • 8
    "3. In the Attributes inspector, find the field labeled Identifier and type MealTableViewCell. Press Return. This is an important step—you’ll see why later." - have you done that? Commented Jan 11, 2016 at 20:55
  • 1
    Yes I have done that, but it still throws an error... I don't know why Commented Jan 11, 2016 at 20:59
  • Can you please take screenshots of your storyboard where you did set the identifier just to be sure. Commented Jan 11, 2016 at 21:02
  • 1
    Well... it looks like I've fixed it. All I had to do was delete the identifier i had typed in and re-enter it again... wish I'd tried this earlier... Thanks for your help anyway! Commented Jan 11, 2016 at 21:03

4 Answers 4

33

This works for me..

my Scene

Attributes inspector

and I'm using a different identifier, "DeviceDetailsCell"

let cellIdentifier = "DeviceDetailsCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! DeviceDetailsTableViewCell
Sign up to request clarification or add additional context in comments.

Comments

12

Just for the record, here's how I solved my issue:

I took the current identifier that was in the attributes inspector, deleted it, pressed return and clicked away. After that, I clicked back into the identifier text box and re-typed the identifier and pressed return. I then saved the storyboard file and it worked when I ran it.

2 Comments

Jan's answer is pretty good in that it has screen shots showing the solution (I had the same problem and managed to fix it this way.) You could consider giving him the accept mark to help him gain another 15 points!
What did it for me is to delete what was in the identifier text box -> Build -> then retype what was in the identifier text box -> Build. Problem went away.
2

In my case I was taking the UITableViewCell from a separate xib file (i.e., not inserting the cell directly in the tableView in storyBoard) and I forgot to properly register the cell in the tableView in this way:

self.tableView.register(UINib(nibName: "NAME_OF_THE_CELL_CLASS", bundle: nil), forCellReuseIdentifier: "REUSE_IDENTIFIER");

Comments

0

Swift (5.0)

I had the same problem but in my case the view was a xib, the way I solved it was:

Bundle.main.loadNibNamed("MealTableViewCell", owner: self, options: nil)?.first as! MealTableViewCell

the complete code would be the following:

import UIKit

class MealTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

        // Configure the view for the selected state
    }

}


extension NameViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Bundle.main.loadNibNamed("MealTableViewCell", owner: self, options: nil)?.first as! MealTableViewCell
        return cell
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.