demar
demar

Reputation: 514

Xcode error: unable to dequeue a cell with identifier MealTableViewCell

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

Upvotes: 18

Views: 16174

Answers (4)

Juanes30
Juanes30

Reputation: 2556

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
    }
}

Upvotes: 0

Andrea Gorrieri
Andrea Gorrieri

Reputation: 1772

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

Upvotes: 2

Jan
Jan

Reputation: 945

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

Upvotes: 33

demar
demar

Reputation: 514

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.

Upvotes: 12

Related Questions