Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static cells - Outlets are always nil

It's the first time I use static cells in a table view and I just can't get them to show.

I have two sections (grouped) with two and three cells in each one respectively. The cells are static (custom) and all carry one label and one textfield:

enter image description here

I have created IBOutlets for each textfield, and for each cell in my view controller:

enter image description here

I have made the link between storyboard -> view controller where the outlets reside:

enter image description here

I have also set datasource & delegate on UITableView:

enter image description here

Since these are static cells, I am not using dynamic cell-related methods, e.g.tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath). Instead, I try to reference each textfield and cell via their outlets. My problem is that I keep getting a nil value returned for all the outlets. I tried referencing them in viewDidLoad, viewWillAppear and viewDidAppear but the result is always nil:

enter image description here

I programmatically instantiate the UIViewController via the use of a cocoa menu control:

enter image description here

What on earth am I missing? Any clues?

  • Note: I am Swift 2.1, Xcode 7.2
like image 818
ppp Avatar asked Dec 07 '25 05:12

ppp


2 Answers

The problem is actually obvious. It's like you have not instantiated it. That's why it's all nil and you can't use it. Since you are using Nib file, you will need to have a way to load the Nib file and then reference it. When you are accessing this UIViewController from Storyboard, the system will take care this for you. What I am suspecting is, you programmatically create this UIViewController.

From another prospective, tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) is not for dynamic cells only. You can pass in the total number of your static cells and you can still use this method to perform the operation you want.

like image 75
Lucas Huang Avatar answered Dec 09 '25 03:12

Lucas Huang


The problem was that I wasn't instantiating the UIViewController from the Storyboard.

Before (all outlets were nil):

enter image description here

After (worked):

enter image description here

like image 29
ppp Avatar answered Dec 09 '25 02:12

ppp