在我的UITableView
我正在使用自定义UITableViewCell
。这些单元中的每一个都具有许多标签。当用户选择一个单元格时,我需要捕获这些标签中一个的内容,但我不知道该怎么做。这是我的代码。我用来尝试获取此标签文本的行基本上是伪代码,显然无法编译。有人可以告诉我这里需要做什么吗?谢谢!
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
cell.groupDescriptionLabel.text = group.group_descr;
cell.groupIDLabel.text = [group.group_id stringValue];
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
Groups *group = [self.fetchedObjects objectAtIndex:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(@"%@", group.group_descr);
...
}
答案 0 :(得分:2)
从视图中获取数据通常是个坏主意。您不应该使用任何视图对象作为存储信息的方式。
当您使用cellForRowAtIndexPath
方法时,您已经获得了字符串。
你应该能够在didSelectRowAtIndexPath
中做同样的事情来获得相同的字符串。
这样你根本不需要从标签中取出文字。
由于
答案 1 :(得分:1)
如果您知道celForRowAtIndexPath将返回您的一个自定义单元格类型而不是通用UITableViewCell,请将结果强制转换为您的自定义单元格类:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// capture the user selection
MyCellClass *selectedCell = (MyCellClass *) [tableView cellForRowAtIndexPath:indexPath];
NSString *selection = selectedCell.groupDescriptionLabel.text; //<-- pseudo-code
NSLog(@"%@", selection);
//...
}