Legatro
Legatro

Reputation: 3732

Unable to dequeue cell with identifier error despite correctly identified cell

I have created two UITableViewCell objects (FriendsCell, AddFriendsCell), and have the following code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == self.selectedFriends.count {
        let cell = tableView.dequeueReusableCell(withIdentifier: "AddFriendsCell", for: indexPath) as! AddFriendsCell
        return cell

    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FriendsCell", for: indexPath) as! FriendsCell
     ...

AddFriendsCell has a button which, when clicked, should segue to another View Controller. However, when clicked it returns this well documented error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'unable to dequeue a cell with
identifier AddFriendsCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'
*** First throw call stack:

I can't see why this error exists when the corresponding prototype cell is very clearly identified as "AddFriendsCell" in it's storyboard settings and is of class type AddFriendsCell (a UITableViewCell class)

When I proceed the first dequeueReusableCell line with:

tableview.register(AddFriendsCell, forCellReuseIdentifier: "AddFriendsCell")

The result is at runtime it produces a blank cell in place of the previously correctly formatted AddTableCell.

Please help me understand why this error is being thrown when this button in AddFriendsCell is pressed, and how to correct it.

Upvotes: 0

Views: 199

Answers (1)

mhergon
mhergon

Reputation: 1678

You need to register cell before use it. For example:

override func viewDidLoad() {
    super.viewDidLoad()

    var nib = UINib(nibName: "AddFriendsCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "AddFriendsCell")

}

UPDATE: Use if you don't have xib file

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(AddFriendsCell.self, forCellReuseIdentifier: "AddFriendsCell")

}

Upvotes: 1

Related Questions