robz
robz

Reputation: 109

Outlets are always nil in custom UITableViewCell

I have a custom UITableViewCell that I designed with interface builder to have a label and a picker. To the implementation swift file, I've added two outlets to refer to the label and picker. The outlets are hooked up to the ui elements and the code looks like this

class TitlePickerCell: UITableViewCell
{

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var picker: UIPickerView!

    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
    }

}

I have a UITableViewController that I want to use this cell with. I set it to static because I'm using it to display settings and the number of cells will never change programmatically. I add one of my custom cells to the table view and set its identifier to MediaStyleCell.

Then, in my swift file for the UITableViewController, I add this line to my viewDidLoad function

self.settingsTable.registerNib(UINib.init(nibName: "TitlePickerCell", bundle: nil), forCellReuseIdentifier: "MediaStyleCell")

where TitlePickerCell is the name of the .xib file and MediaStyleCell is the identifier that I gave the cell with interface builder. When I run my app, it crashes because the label and picker in my custom cell class are nil.

Upvotes: 2

Views: 7281

Answers (4)

Vladimir Kuzomenskyi
Vladimir Kuzomenskyi

Reputation: 521

If you are using tableview.registerClass:forCellReuseIdentifier: but you have cell's xib file also, you will get this error.

So instead of

tablView.register(Cell.self, forCellReuseIdentifier: cellToDisplay.identifier)

just use

tableView.register(UINib(nibName: cellToDisplay.identifier, bundle: nil), forCellReuseIdentifier: cellToDisplay.identifier)

Upvotes: 7

fatuous.logic
fatuous.logic

Reputation: 750

Only reason I can think for this to happen is if the outlets are not linked correctly, the incorrect nib/identifier is being registered or the reuse identifier in the .xib file is missing/incorrect.

Unable to reproduce, here is VC code I used:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib.init(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "DifferentIdentifier")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 44
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("DifferentIdentifier", forIndexPath: indexPath) as! CustomTableViewCell
        cell.titleLabel.text = "HERPITY"
        return cell
    }
}

here is cell:

class CustomTableViewCell: UITableViewCell {

    @IBOutlet var titleLabel: UILabel!

    @IBOutlet var pickerView: UIPickerView!

    override func awakeFromNib() {
        super.awakeFromNib()
        titleLabel.text = "Works"
        pickerView.backgroundColor = UIColor.blueColor()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

and I get this nice ugly cell (I just made it blue cos it was first auto-complete)

enter image description here

Upvotes: 2

Eric
Eric

Reputation: 1213

Make sure the outlets are actually connected:

Open the xib file and on the top right side, go to the outlets menu. Connect the outlets in there.

outlets connection

Upvotes: 0

robz
robz

Reputation: 109

It seems that creating a custom UITableViewCell is the wrong way to go about this. Just setting the cell style to custom and adding UI elements that way is what should be done.

Upvotes: -3

Related Questions