Edward Hasted
Edward Hasted

Reputation: 3433

Returning a value from a TableViewCell

If I click on a TableViewCell I can return the cell number with:

indexPath.row+1

What I want to return is the value of one of the labels in the cell. This avoids me having to create an array in parallel and pulling the answer from that.

Can it be done?

Upvotes: 0

Views: 34

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130212

You can use cellForRowAtIndexPath: to get a reference to a specific cell in a tableview by passing in its index path, which is known in didSelectRowAtIndexPath:. Once you have a reference to this cell, you can access whichever of its properties you want. Heres an example of how to get the text from each of a cell's labels.

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

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

Upvotes: 3

Related Questions