views:

21

answers:

2

How do I see which cell is selected in a UITableView? I'm just trying to do something simple, but I kinda forgot how to do it. :D Anyway, could some one help me? I want it to be an "if" statement such as:

if (/*The first cell got selected*/) {
    self.label.text = @"Hello!"
}

Could someone fill out the commented space?

Thanks

A: 
//table is a reference to your UITableView
NSIndexPath* path = [table indexPathForSelectedRow];
if (path && path.row == 0 ) {
    self.label.text = @"Hello!"
}

You can also track changing selected state in UITableView's delegate tableView:didDeselectRowAtIndexPath: method

Vladimir
What class is table? It's undeclared.
Nathan
you have to replace `table` with your table's outlet (`self.tableView` may work depending on your configuration)
jrtc27
+1  A: 

Presumably you want to do this when the user makes their selection? If so, you need to implement - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath in your table view delegate:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    if (indexPath.row == 0) {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        cell.textLabel.text = @"Hello!";
    }
    // ... other row selection logic
}
chrispix