0

i'm currently facing an error in my swift file that nothing from the other threads on this subject solved.

I created a tableview thanks to my NewsTableViewController, I created a prototype for my custom cells that I identified as cell in the main storyboard. The Controller is alone and linked with no segue just as a prototype for display like this:

Controller on the right is my table view controller that bugs

But when I execute the application, when I click on the button that launches the piece of code that instantiate the NewsTableViewController, I have the error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

The identifier of my prototype is good yet, so what is wrong? Here is the code of my NewsTableViewController :

class NewsTableViewController: UITableViewController {

var listOfDisplays = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func fetchEventsNames()
{
    let fetchRequest:NSFetchRequest<Events> = Events.fetchRequest()
    do {
        let results = try DataBaseControler.getContext().fetch(fetchRequest)
        for i in results as [Events]{ /* *dewraper les arguments (i.name)!*/
            listOfDisplays.append(i.title!)
        }
    }
    catch
    {
        print("errors\(error)")
    }
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifier = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! NewsTableViewCell
    cell.cellImage.image = UIImage(named: "rcbl.jpg")
    cell.cellLabel.text = listOfDisplays[indexPath.row]
    return(cell)
}

Here is also the code of my NewsTableViewCells :

class NewsTableViewCell: UITableViewCell {

@IBOutlet weak var cellLabel: UILabel!
@IBOutlet weak var cellImage: UIImageView!
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
}

}

And then here is the piece of code where I call the constructor:

...
    switch (names[row])
        {
    case "Actualités":
        let destination = NewsTableViewController() // Your destination
        destination.listOfDisplays = ["test1", "TEST2"]
        navigationController?.pushViewController(destination, animated: true)...

What should I do to solve this problem ? Thank you in advance guys.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Elbattore
  • 86
  • 8
  • Possible duplicate of [Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:](https://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath) – mfaani Jun 08 '17 at 16:11
  • 1
    Is the identifier `cell` properly set in Interface Builder? And **do not** call the default initializer `NewsTableViewController()`, that creates a brand new instance which is **not** the instance designed in Interface Builder. You need the actual reference to the controller. Most likely that's the error reason. – vadian Jun 08 '17 at 16:15
  • I agree with vadian. `let destination = NewsTableViewController()` that's not calling the corresponding xib (which is inside the Storyboard), and then it doesn't know the cell and causes the crash. At least call `storyboard.instantiateViewControllerWithIdentifier()` to do it, and then it should not crash. – Larme Jun 08 '17 at 16:22

1 Answers1

1

When you create the view controller like this:

let destination = NewsTableViewController() // Your destination

you are just creating an instance of that class with none of the setup you have done in the storyboard.

You either need to instantiate one from the storyboard with something like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil) // Use the appropriate story board name
let destination = storyboard.instantiateViewController(withIdentifier: "testtable") // Make sure you have allocated the correct storyboard id to the view controller
self.navigationController?.pushViewController(destination, animated: true)

or you can register you cell class for the table view in the viewDidLoad() like this:

self.tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "testcell") // Use the correct class and identifier.
Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23