Search code examples
swiftuitableviewxib

TableViewCell Nib error: loaded the nib but the view outlet was not set


Trying to create a tableView cell xib, but am stuck with the error "loaded the nib but the view outlet was not set"

I have read many posts and most say to drag the "view" from File's Owner to the IB view.

However, if you notice from the images, I do not have a "view" as an option. I have recreated a xib with the same results. Maybe new in Swift 4.2.

Any help would be huge.

class VersionTVCell: UITableViewCell {

    @IBOutlet weak var  versionNumber: UILabel!
    @IBOutlet weak var  versionDetail: UILabel!
    @IBOutlet weak var  versionDate: UILabel!
}


class VersionTVC: UITableViewController {

    fileprivate var versions: Array<VersionModel> = [VersionModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.versions = AppDelegate.getRLDatabase().getVersion()

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

        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 70
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "VersionCell", for: indexPath) as! VersionTVCell

        cell.versionNumber.text              =   self.versions[(indexPath as NSIndexPath).row].versionNumber
        cell.versionDetail.text              =   self.versions[(indexPath as NSIndexPath).row].versionDetail
        cell.versionDate.text                =   self.versions[(indexPath as NSIndexPath).row].versionDate

        return cell
    }

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

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.versions.count
    }
}

Below are two screen shots, first from the cell and second from the File's Owner.

First Screen shot

Second Screen shot

I have tried linking from File's Owner to the labels as well. Did not help.

EDIT: Adding my addSubView func:

@objc func loadVersions() {

    let newSubView = AddSubView.toVersions.getView()
    let blackBackground = UIView(frame: CGRect(x: 0, y: 65, width: self.view.frame.width, height: self.view.frame.height))
    blackBackground.backgroundColor = UIColor.black.withAlphaComponent( 0.7)

    UIView.transition(with: self.view, duration: 0.3, options: UIView.AnimationOptions.transitionCrossDissolve,
                      animations: {self.view.addSubview(self.backgroundView)}, completion: nil)

    newSubView.frame = CGRect(x: 0, y: (self.view.frame.height) * 0.1, width: (self.view.frame.width) * 0.9, height: (self.view.frame.height) * 0.8)
    newSubView.layer.borderWidth = 1
    newSubView.layer.borderColor = UIColor.defaultDialogBorderColor().cgColor
    newSubView.layer.cornerRadius = 8.0
    newSubView.clipsToBounds = true
    self.view.addSubview(newSubView)
}

Screenshot of VersionTVC


Solution

  • I have tried your code and it's just working fine.

    Make sure you do not assign VersionTVCell of UITableViewCell class to file owner and not connecting @IBOutlet to xib

    enter image description here

    File Owner connections Inspector should look alike below image

    enter image description here

    Also, try deleting and reconnecting @IBOutlet

    enter image description here

    Tested example

    enter image description here