在didSelectRowAtIndexPath中使用“cell”

时间:2012-08-20 07:56:24

标签: ios uitableview

我可以在我的CellForRowAtIndexPath中使用“cell.text”,但是当我尝试在didSelectRowAtIndexPath上使用它时,它表示未声明的“cell”

我是否必须再次申报或如何解决此问题?

2 个答案:

答案 0 :(得分:2)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;回调中,您没有任何cell引用。您可以通过- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;例程来获取它,如下所示:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    //whatever you want to do with the cell
}

但是无论如何,当您之前创建过单元格时,您应该能够使用indexPath中的行获取基础数据。这样的事情:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    SomeDataObject* object = [someDataArray objectAtIndex:row];
    //whatever you want to do with the data
}

答案 1 :(得分:0)

应该没有必要尝试访问didSelectRowAtIndexPath中的单元格。您很可能将用于填充存储在array或类似内容中的表数据的数据放在一起,这样您就可以使用indexPath变量从array访问它。 [indexPath row](根据您的设置,您可能还需要考虑[indexPath section]。)

相关问题