Search code examples
uitableviewuikitstoryboard

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


I have created custom UITableViewCell, in storyboard for my TableViewController I set my cell as a custom class and also give it an identifier. And in controller used dequeueReusableCell method .But when I try to launch my app it crashes throwing this error: "unable to dequeue a cell with identifier BeerCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard". I have watched many tutorials and everywhere described same steps what I have been done.Have no idea what I'm doing wrong.Thanks in advance enter image description here

enter image description here

class BeerCell: UITableViewCell {
    @IBOutlet var beerName: UILabel!
    @IBOutlet var beerABV: UILabel!
    @IBOutlet var addToFavoritesButton: UIButton!
    
    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
    }
    
}

class BeersListTableViewController: UITableViewController{        
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BeerCell", for: indexPath) as! BeerCell
        cell.beerName?.text = "name"
        cell.beerABV?.text = "12.0"
        cell.addToFavoritesButton?.setImage(UIImage(systemName: "star"), for: .normal)
        return cell
    }

Solution

  • Looks like you forgot to register your cell. Add this line to UITableViewController's viewDidLoad():

    tableView.register(UINib(nibName: "BeerCell", bundle: nil), forCellReuseIdentifier: "BeerCell")