iosswiftuitableviewstoryboardnib

Storyboard UITableViewCell could not load nib in bundle with name... when loaded programmatically


I have a Viewcontroller scene (Storyboard) with a UITableView and inside a UITableViewCell called Foo. Foo loads fine when loaded from a storyboard, but I need to load it from another UITableView programmatically.

I'm registering the cell like this (at first I tried without UINib but IBOutlets were nil):

override func viewDidLoad() {
    super.viewDidLoad()        
    tableView.register(UINib(nibName: "Foo", bundle: nil), forCellReuseIdentifier: "bar")
}

and then loading it like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "bar", for: indexPath) as! Foo
    return cell
}

This doesn't work and returns the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <[path]> (loaded)' with name 'Foo''

Things I made sure of:


Solution

  • You may want to create a separated nib file for your cell then register it to your table view like what you've done above.