首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView DidSelectRowAtIndexPath?

UITableView DidSelectRowAtIndexPath?
EN

Stack Overflow用户
提问于 2011-12-07 02:32:02
回答 5查看 71.4K关注 0票数 25

我有一个包含几个项目(全文本)的表视图。当用户单击行时,我希望将该单元格中的文本添加到一个数组中。

如何从单元格中抓取文本?

我已经有了:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

有没有一条简单的行可以用来抓取用户单击的单元格中的文本?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-12-07 02:34:59

使用UITableView's cellForRowAtIndexPath:

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
}
票数 94
EN

Stack Overflow用户

发布于 2011-12-07 02:36:00

这与您在cellForRowAtIndexPath中所做的事情相反。

代码语言:javascript
复制
 [myArray addObject: [datasource objectAtIndex:indexPath.row]];
票数 6
EN

Stack Overflow用户

发布于 2011-12-07 02:36:11

通常,您可以在Apple文档中找到此类问题的大多数答案。在这种情况下,查看UITableView Class Reference下,您将看到一个小节标题: Accessing Cells and Sections。这为您提供了以下方法:

代码语言:javascript
复制
– cellForRowAtIndexPath:

现在使用此命令访问单元格,然后使用get the text from the cell (如果您不确定如何访问单元格,请查看UITableViewCell Class Reference

总而言之,您的方法可能如下所示:

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    [myArray addObject:cellText];
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8404922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档