如何在尝试检索选择的表格单元格时正确实现 - (NSIndexPath *)indexPathForSelectedRow

时间:2010-07-01 00:45:23

标签: iphone uitableview delegates

我有一个表格视图的委托,并且希望能够在用户选择tableview中的单元格时触发操作。

如何正确实现方法 - (NSIndexPath *)indexPathForSelectedRow?

1 个答案:

答案 0 :(得分:2)

如果您已正确设置表委托(请参阅Apple的AdvancedTableViewCells教程或任何数量的好教程以获取更多信息),那么您可以通过实施此方法来遵循Apple的协议:

- (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];

}   

这些只是一般性的例子,但希望他们能为你阐明这个主题!