从TableViewCell返回一个值

时间:2014-01-23 13:30:47

标签: ios uitableview

如果我点击TableViewCell,我可以使用以下命令返回单元格编号:

indexPath.row+1

我想要返回的是单元格中某个标签的值。这避免了我必须并行创建一个数组并从中得出答案。

可以吗?

1 个答案:

答案 0 :(得分:3)

您可以使用cellForRowAtIndexPath:通过传入其didSelectRowAtIndexPath:中已知的索引路径来获取对tableview中特定单元格的引用。获得此单元格的引用后,您可以访问所需的任何属性。下面是一个如何从每个单元格的标签中获取文本的示例。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    NSString *labelText = cell.textLabel.text;
    NSString *detailText = cell.detailTextLabel.text;
}