TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Help with UIKit Error - unable to dequeue a cell with identifier

Forums > Swift

Hey, I'm new to UIKit and I'm having some trouble running this UITableViewController.

I keep getting this error - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier albumCellReuse - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' terminating with uncaught exception of type NSException

I am not using storyboard, instead the user interface is done programmatically.

These are the files, please let me what's the cause of this error. Thank You! -

File - TableViewController

import UIKit

func CreateAlbum() -> Array<TableModel>{

    let darkSide = TableModel(albumName: "Dark Side Of The Moon", songCount: 10, artistName: "Pink Floyd", releaseDate: 1973, rating: .wow)
    let borntorun = TableModel(albumName: "Born To Run", songCount: 10, artistName: "Bruce Springsteen", releaseDate: 1972, rating: .wow)
    let lifeOfPablo = TableModel(albumName: "Life Of Pablo", songCount: 20, artistName: "Kanye West", releaseDate: 2016, rating: .great)
    let damn = TableModel(albumName: "DAMN", songCount: 8, artistName: "Kendrick Lamar", releaseDate: 2017, rating: .good)
    return [darkSide,borntorun,lifeOfPablo,damn]
}

class TableViewController: UITableViewController {

    var table: UITableView = UITableView()
    var reuseIdentifier: String = "albumCellReuse"
    var albums: Array<TableModel> = []

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "Table View!!"
        view.backgroundColor = UIColor.white
        albums = CreateAlbum()

        table.translatesAutoresizingMaskIntoConstraints = false
        table.dataSource = self
        table.delegate = self
        table.register(CustomTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        view.addSubview(table)
        SetUpConstraints()
    }

    func SetUpConstraints() {

        NSLayoutConstraint.activate([
            table.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
            table.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            table.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return albums.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as? CustomTableViewCell {
            let album = albums[indexPath.row]
            cell.configure(album: album)
            cell.selectionStyle = .none
            return cell

        }
        else {
            return UITableViewCell()
        }
}

}

3      

I fixed it, if anyone's wondering!

Instead of tableView it should just be table.

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      if let cell = table.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as? CustomTableViewCell {
          let album = albums[indexPath.row]
          cell.configure(album: album)
          cell.selectionStyle = .none
          return cell

      }
      else {
          return UITableViewCell()
      }
}

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.