Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype cell from storyboard not creating UILabels, etc

My question is very similar to this one.
My custom prototype cells designed in Interface Builder using storyboards have all the internal objects set to nil (not created), so when I try to assign values to them, they remain blank.

I am using Xcode 4.6.2, and running the code in the 6.1 iPhone simulator. Here is what I've done:

  • designed a prototype cell in interface builder with the necessary fields.
  • created a subclass of UITableViewCell for this custom cell in code, and then set the cell in interface builder to this type.
  • control-dragged the fields into the .h file, which set up the objects (UILabels, etc) for me. I set the identifier of the cell to "serverNameCell"
  • set the datasource & delegate of the table to the Viewcontroller that the table is in.
  • associated the table with a table object in the ViewControler

At the moment, the table displays with the correct number of sections & rows, but the values of the cell are not being set.

This is the cellForRowAtIndexPath:(NSIndexPath *)indexPath code:

NewServerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"serverNameCell"];

I always get back a cell, and the memory location seems to be ok for a valid object.

But when I try to cell.name.text = [thisServer name]; I find that the cell.name label is always nil.
There are no crashes - but my fields are not being set.

I have checked a million times that the identifier is ok - it is! I have gone through the Apple documentation for custom cells from a storyboard - but still get this frustrating issue..

Anyone else had this, and resolved it?

like image 844
DefenestrationDay Avatar asked May 12 '13 03:05

DefenestrationDay


2 Answers

I had this exact problem today with Xcode 5's storyboard. After pulling almost all my remaining hair out, I noticed I was calling this in viewDidLoad:

[self.tableView registerClass:[SwitchTableViewCell class] forCellReuseIdentifier:@"Setting"];

Whoops! This line is unnecessary when using prototype cells in storyboards. The storyboard automatically configures this for me. SwitchTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Setting" forIndexPath:indexPath]; works just fine without calling registerClass:: explicitly.

The question still remains of why calling registerClass:: causes some outlets to not be connected.

like image 188
NathanAldenSr Avatar answered Oct 19 '22 14:10

NathanAldenSr


An easy workaround for this problem is to tag the UILabel in the prototype and manipulate it with:

UILabel* label = [cell viewWithTag:cellTag];
label.text = [thisServer name]
like image 40
hugey Avatar answered Oct 19 '22 15:10

hugey