ari gold
ari gold

Reputation: 2094

access text in cell.textLabel.text

This seems like a simple thing but I'm stuck.

In my storyboard, I have a cell with text in it's label (using Static Cells). In didSelectRowAtIndexPath, I'm trying to access the cell's textLabel's text. Here's my code, with NSLogging:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.textLabel.text) {
      NSLog(@"cell.textLabel.text = %@",cell.textLabel.text);
  } else {
      NSLog(@"!cell.textLabel.text");
  }
NSLog(@"just for fun: cell.textLabel.text = %@", cell.textLabel.text);

The if statement always returns "!cell.textLabel.text" and the last NSLog is always (null) although the storyboard's cell has text in it.

Is cell.textLabel not correct? Should I be looking at another subview of UITableViewCell?

Upvotes: 2

Views: 1196

Answers (1)

Jimmy Luong
Jimmy Luong

Reputation: 1109

The default cell style of table cells in the Storyboard are of type 'Custom'. Which gives you a blank cell where you can add other types of controls to it.

If you dragged a label onto the cell and changed its text, then you are likely using a Custom style of UITableViewCell.

The property textLabel is only available on cells of types other than Custom, such as Basic, or Detailed.

Therefore you should first check out if these styles meet your requirements and use them instead.

If you really do require a Custom type of cell, you will need to make a subclass of UITableViewCell and create outlets to access your label.

Upvotes: 1

Related Questions