views:

33

answers:

1

I have a delegate for my table view, and would like to be able to trigger an action when the user selects a cell in the tableview.

How is the method - (NSIndexPath *)indexPathForSelectedRow correctly implemented?

Thanks in advance.

+1  A: 

If you have the table delegates set up properly (see Apple's AdvancedTableViewCells tutorial or any number of good tutorials out there for more info), then you follow Apple's protocol by implementing this method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   

    // in here you can do stuff based on which cell they selected
    // at a certain index path, like the examples below

    // get value for which row was pressed
    int row = indexPath.row;

    // or find cell that was just pressed and grab handle for the new info that needs to be put on it
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    UIButton *newButton;
    newButton = (UIButton *)[cell viewWithTag:4];

    UILabel *newLabel;
    newLabel = (UILabel *)[cell viewWithTag:5];

    // or de-select the row per Apple's Human Interface Guidelines 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}   

Those are just generic examples, but hopefully they shed a little light on the subject for you!

iWasRobbed
perfect, thanks a lot.
alJaree