访问cell.textLabel.text中的文本

时间:2020-12-13 10:43:55

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:

在我的故事板中,我有一个带有文本标签的单元格(使用静态单元格)。在didSelectRowAtIndexPath中,我正在尝试访问单元格的textLabel文本。这是我的代码,使用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.

if语句始终返回“!cell.textLabel.text”,最后一个NSLog始终为(null),尽管故事板的单元格中包含文本。

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

cell.textLabel不正确吗?我应该查看UITableViewCell的另一个子视图吗?

1 个解决方案

#1


1  

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.

Storyboard中表格单元格的默认单元格样式为“Custom”。这为您提供了一个空白单元格,您可以在其中添加其他类型的控件。

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

如果您将标签拖到单元格上并更改了其文本,那么您可能正在使用自定义样式的UITableViewCell。

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

属性textLabel仅适用于Custom之外的其他类型的单元格,例如Basic或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.

如果您确实需要自定义类型的单元格,则需要创建UITableViewCell的子类并创建出口来访问您的标签。

#1


1  

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.

Storyboard中表格单元格的默认单元格样式为“Custom”。这为您提供了一个空白单元格,您可以在其中添加其他类型的控件。

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

如果您将标签拖到单元格上并更改了其文本,那么您可能正在使用自定义样式的UITableViewCell。

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

属性textLabel仅适用于Custom之外的其他类型的单元格,例如Basic或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.

如果您确实需要自定义类型的单元格,则需要创建UITableViewCell的子类并创建出口来访问您的标签。