Swift3 - First Letter of Search Crashing Table View

83 Views Asked by At

I am getting this error when the first line of my "else" block runs:

unable to dequeue a cell with indentifier PhoneCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

So, the initial table is loading just fine, but when you try to do a search this exception is thrown. This only started happening after I went to a custom cell. Here is the cellForRowAtIndexPath function for the table that is crashing. Any idea what's going on?

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


    if tableView == self.tableView{
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath) as! PhoneSearchResultTableViewCell
        cell.nameLabel.text = phones[indexPath.row].name.value
        cell.descriptionLabel.text = phones[indexPath.row].descriptionText.value
        cell.modelLabel.text = phones[indexPath.row].model.value
        return cell
    }else{
        let cell = self.resultsController.tableView.dequeueReusableCell(withIdentifier: "PhoneCell", for: indexPath) as! PhoneSearchResultTableViewCell
        cell.nameLabel.text = filteredPhones[indexPath.row].name.value
        cell.descriptionLabel.text = filteredPhones[indexPath.row].descriptionText.value
        cell.modelLabel.text = filteredPhones[indexPath.row].model.value
        return cell
    }
}
2

There are 2 best solutions below

1
Brandon A On BEST ANSWER

Your UITableViewController does not have either a prototype cell in it's storyboard for you "PhoneCell" or if you are doing it programmatically you did not register the class for the cell identifier you are calling.

Try adding this in your UIViewController's viewDidLoad or somewhere before you instantiate your UITableView:

self.resultsController.tableView.register(PhoneSearchResultTableViewCell.self, forCellReuseIdentifier: "PhoneCell")
0
Dmitry On

It seems that you are using UITableViewController rather than a separate UITableView. If you created that custom cell as a prototype cell in IB, then make sure that the reuse identifier is set the same in the code and in the IB.

UITableViewController automatically registers the prototype cells.

However, if you start using UITableView separately, then you need to call .register method with either UINib or class of your custom cells.