iosswiftuinib

'NSInternalInconsistencyException' reason: 'Could not load NIB in bundle' when dequeuing tableview cell


I have no clue why I am getting this error and I have tried looking at other answers, but could not find a solution.

The code in question:

override func viewDidLoad() {
    super.viewDidLoad()

    messagesTableView?.register(UINib.init(nibName: "messageTableViewCell", bundle: nil), forCellReuseIdentifier: "messageTableViewCell")

    messagesTableView.delegate = self;
    messagesTableView.dataSource = self;
    dataSrc.delegate = self


}

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


    if let cell = tableView.dequeueReusableCell(withIdentifier: "messageTableViewCell", for: indexPath) as? MessagesTableViewCell {

        cell.configureWithItem(item: dataArray[indexPath.item])

        return cell
    }
    return UITableViewCell()
}

When tableView.dequeueReusableCell is called I get the error. I have also made sure the identifier for the cell is set to "messageTableViewCell"


Solution

  • When you're initializing UINib, as nibName parameter you have to pass name of the file

    enter image description here

    UINib(nibName: "TableViewCell", bundle: nil)
    

    so in your case I suppose your file is named same as your class MessagesTableViewCell

    messagesTableView?.register(UINib(nibName: "MessagesTableViewCell", bundle: nil), forCellReuseIdentifier: "messageTableViewCell")