Custom table view cell: IBOutlet label is nil

IosUitableviewSwiftInterface Builder

Ios Problem Overview


Consider the following simple view controller:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    
    var items = ["One", "Two", "Three"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
        self.tableView.dataSource = self
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
        cell.titleLabel!.text = self.items[indexPath.row]
        return cell
    }
}

And custom cell view:

class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!   
}

This code causes the following error.

> fatal error: unexpectedly found nil while unwrapping an Optional value

titleLabel is nil — it's not clear why. Setting default properties of UITableViewCell (like textLabel) work just fine.

I'm using a nib for the custom cell.

Both the labels and table views are correctly connected to their IBOutlets.

label table view

Both the prototype cell and the custom nib view are marked as having a CustomTableViewCell class.

I'm new to iOS development, am I missing something obvious?

Sample Xcode project available.

Ios Solutions


Solution 1 - Ios

First off, you're using a nib file to load your custom cell into the table. That's probably going to be more of a headache than it's worth if you're new to Swift/Cocoa. I would move everything over to storyboard for the time being. Instead of using a nib file click, go to Storyboard, click on your UITableView and make sure the TableView's content setting is Dyanamic Prototypes:

enter image description here

Next, click on the prototype cell (the only one in the table view) and set the class to CustomTableViewCell and set its reuse identifier to customCell:

enter image description here enter image description here

Next, add a label to your prototype cell and link it to the IBOutlet in your CustomTableViewCell class. You don't need to register your customCell so long as you've set the reuse identifier in storyboard. Delete this line:

self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")

and it should run.

Solution 2 - Ios

try this

import UIKit

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!
    
    var items = ["One", "Two", "Three"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")// CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
        self.tableView.dataSource = self
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as CustomTableViewCell
        cell.titleLabel!.text = self.items[indexPath.row]
        return cell
    }
}

Solution 3 - Ios

Register your nib like this:

let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")

Solution 4 - Ios

You should use dequeueReusableCellWithIdentifier:forIndexPath to dequeue the cells.

If you are using storyboard to create the cells, you should not need to register the class for reuse as storyboard does it for you if you set the reuseIdentifier.

Solution 5 - Ios

Please make sure you are not doing any mistake while registering your nib(custom cell) in the viewdidload. Nib name and reuseIdentifiers should not be wrong.

Solution 6 - Ios

Works for me without !

EDIT

    let challenge = self.challenges![indexPath.row]

    let title = challenge["name"]
    if title != nil {
        cell.titleLabel.text = title
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid ChouinardView Question on Stackoverflow
Solution 1 - IoskellanburketView Answer on Stackoverflow
Solution 2 - IosLDNZhView Answer on Stackoverflow
Solution 3 - IosArnavView Answer on Stackoverflow
Solution 4 - IosRory McKinnelView Answer on Stackoverflow
Solution 5 - IosNarasimha NallamsettyView Answer on Stackoverflow
Solution 6 - IosiOSfleerView Answer on Stackoverflow