Iphone 如何获取UITableview中选定行的标题

Iphone 如何获取UITableview中选定行的标题,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我有tableview,每个单元格上都有一些名称,当选择行时如何获得该名称 我知道我必须使用委托方法 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 但是我怎样才能得到那一行的标题呢 提前谢谢 关于假设您使用的是标准UITableViewCell,则可以使用 UITableViewCell *cell = [self.tableView cellForRowA

我有tableview,每个单元格上都有一些名称,当选择行时如何获得该名称

我知道我必须使用委托方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
但是我怎样才能得到那一行的标题呢

提前谢谢


关于

假设您使用的是标准UITableViewCell,则可以使用

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
然后通过以下方式访问文本属性视图属性:

cell.textLabel.text
cell.detailTextLabel.text
此代码段检索所选单元格,并将其
text
属性存储在
NSString



但是,我不建议这样做。最好将数据层和表示层分开。使用备份数据存储(例如数组)并使用传递的
indexPath
对象中的数据(例如用于访问数组的索引)访问它。此数据存储将与用于填充表的数据存储相同。

一旦通过delegate属性将一个类注册为委托,您只需实现
tableView:DidSelectRowatineXpath:
方法,该方法将在用户选择行时调用。(有关详细信息,请参阅。)

然后,您可以通过以下方式从选定单元格中提取标签文本

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellLabelText = cell.textLabel.text;
    }

…如果这是您真正需要的。

您可以使用以下(uitableview-)功能访问uitableview的每一行:

顺便说一下,要使用uitableview的委托方法以外的其他方法访问tableview,必须在IB中连接它(例如..)

希望能有帮助

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

    NSString *rowTitle=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;   
}
这里,


[tableView cellForRowAtIndexPath:indexPath]为您提供所选单元格,然后.textLabel.text为您提供该单元格的标签。

行没有标题。你想做什么?我的意思是UItableview(+1)中的每个单元格都对我有用,即使是小的打字错误。在代码中它是UITableViewCell,您编写了UITableView(但是您在上面的文本中写的是正确的。非常感谢…您救了我的一天…;)太棒了!谢谢。
cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *rowTitle=[tableView cellForRowAtIndexPath:indexPath].textLabel.text;   
}