0

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.

2
  • 1
    You say that you set correctly the class of the prototype cell in your storyboard. In your Storyboard, did you set also its identifier ? Commented Feb 21, 2017 at 14:16
  • Indeed, the class is AddFriendsCell and the identifier is "AddFriendsCell" Commented Feb 21, 2017 at 14:18

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

6 Comments

Cheers. Unfortunately that exact code has lead to an error which I previously encountered: uncaught exception: 'Could not load NIB in bundle: '....(long path)....' with name 'AddFriendsCell''
Try it, it's not the same error you say. You pass class, not an instance like me.
After typing out your code and testing, it gives the same error.
Do you have AddFriendsCell.swift and AddFriendsCell.xib files? Or only AddFriendsCell.swift ?
I have only AddFriendsCell.swift. Infact, I don't see any .xib files in the navigator throughout my project.
|

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.