我有一个包含几个项目(全文本)的表视图。当用户单击行时,我希望将该单元格中的文本添加到一个数组中。
如何从单元格中抓取文本?
我已经有了:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}有没有一条简单的行可以用来抓取用户单击的单元格中的文本?
发布于 2011-12-07 02:34:59
使用UITableView's cellForRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
}发布于 2011-12-07 02:36:00
这与您在cellForRowAtIndexPath中所做的事情相反。
[myArray addObject: [datasource objectAtIndex:indexPath.row]];发布于 2011-12-07 02:36:11
通常,您可以在Apple文档中找到此类问题的大多数答案。在这种情况下,查看UITableView Class Reference下,您将看到一个小节标题: Accessing Cells and Sections。这为您提供了以下方法:
– cellForRowAtIndexPath:现在使用此命令访问单元格,然后使用get the text from the cell (如果您不确定如何访问单元格,请查看UITableViewCell Class Reference。
总而言之,您的方法可能如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = cell.textLabel.text;
[myArray addObject:cellText];
}https://stackoverflow.com/questions/8404922
复制相似问题