0

Firstly, sorry If something I say is wrong as this is my first attempt to work with nibs.

I have a UIViewController "MainVC" that gets Initialised with a nib file (I decided that because it is a popup and made more sense) and I want to embed another view controller "RenderersViewController" within a UIView. For doing that I do:

override func viewDidLoad() {
    super.viewDidLoad()

    let renderersVC = RenderersViewController(withRowHeight: 42)
    addChild(renderersVC)
    renderersVC.view.frame = renderersContainerView.bounds
    renderersContainerView.addSubview(renderersVC.view)
    renderersVC.didMove(toParent: self)

}

RenderersViewController is UIViewController that has a UITableView and it has a custom Initialisation and nib

enter image description here

init(withRowHeight rowHeight:CGFloat) {
    self.rowHeight = rowHeight

    super.init(nibName: "RendererVCNib", bundle: nil)

}

when the tableView in RenderersViewController didSet i Register a custom UITableViewCell that also has it's own nib.

@IBOutlet weak var renderersTableView: UITableView!{
    didSet{
        renderersTableView.register(RenderersTableViewCell.self, forCellReuseIdentifier: "mycell")
        renderersTableView.bounces = false
        renderersTableView.dataSource = self
        renderersTableView.delegate = self
    }
}

and in cellForIndex

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

    let rendererCell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! RenderersTableViewCell

    if indexPath.row < rendererArray.count{

        let cpRendererItem = rendererArray[indexPath.row]

        //rendererCell.rendererNameLabel.text = cpRendererItem.friendlyName
        //rendererCell.rendererImageView.sd_setImage(with: cpRendererItem.iconUrl, placeholderImage: UIImage(named:"upnp_ic")!)

        rendererCell.rendererNameLabel.text = "HELLO"
        rendererCell.rendererImageView.image = UIImage(named:"upnp_ic")

    }

    return rendererCell

}

But it crashes because the IBOutlets are nil... enter image description here

RenderersTableViewCell Nib

enter image description here

enter image description here

enter image description here

Reimond Hill
  • 4,278
  • 40
  • 52

1 Answers1

1

Instead of registering the cell's class, you need to register the nib file, that is used to instantiate the cell:

@IBOutlet weak var renderersTableView: UITableView!{
    didSet{
        let nibFile = UINib(nibName: "cells_xib_file", bundle: nil)
        renderersTableView.register(nibFile, forCellReuseIdentifier: "mycell")
        renderersTableView.bounces = false
        renderersTableView.dataSource = self
        renderersTableView.delegate = self
    }
}

Apart from that your xib file looks a little bit wrong. For a cell xib, it should have kind of UITableViewCell (fro your case RenderersTableViewCell) as a top view component in the hierarchy. Eventually you should get something similar to this.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • Thank you for your answer now I am getting " *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (mycell) - nib must contain exactly one top level object which must be a UITableViewCell instance' " – Reimond Hill Nov 04 '18 at 18:00
  • Yes, it's something i missed. In your storiboard the top object is a common view, but you need to make it kind of UITableViewCell (for your case RenderersTableViewCell) so the view hierarchy should look like this: https://i.imgur.com/TiKdxyy.png – The Dreams Wind Nov 04 '18 at 18:14
  • 1
    Yeei!! With the picture I have seen it a little bit more clear! It does not crash now. I have always struggled to understand what file's owners means. If you attach the picture and a little bit the files owner's stuff i will accept it and upvote it... – Reimond Hill Nov 04 '18 at 18:29
  • @ReimondHill, edited :) – The Dreams Wind Nov 04 '18 at 18:34
  • 1
    Great! Thank you very much!!!:) – Reimond Hill Nov 04 '18 at 19:14
  • 1
    Great! Thank you very much!!!:) – Reimond Hill Nov 04 '18 at 19:14