开发者

How to correctly implement - (NSIndexPath *)indexPathForSelectedRow when trying to retrieve which table cell is selected

I have a delegate for my table view, and would like to be able t开发者_运维百科o trigger an action when the user selects a cell in the tableview.

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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜