Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从多标签UITableViewCell上的一个标签获取文本_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 从多标签UITableViewCell上的一个标签获取文本

Ios 从多标签UITableViewCell上的一个标签获取文本,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在我的UITableView上,我使用的是自定义UITableViewCells。每个单元格都有许多标签。当用户选择一个单元格时,我只需要捕获其中一个标签的内容,但我不知道怎么做。这是我的密码。我用来获取标签文本的代码基本上是伪代码,显然无法编译。有人能告诉我我需要在这里做什么吗?谢谢 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

在我的
UITableView
上,我使用的是自定义
UITableViewCell
s。每个单元格都有许多标签。当用户选择一个单元格时,我只需要捕获其中一个标签的内容,但我不知道怎么做。这是我的密码。我用来获取标签文本的代码基本上是伪代码,显然无法编译。有人能告诉我我需要在这里做什么吗?谢谢

- (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);

    ...    
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
...
Groups*group=[self.fetchedObjects objectAtIndex:indexath.row];
cell.groupDescriptionLabel.text=group.group\u descr;
cell.groupIDLabel.text=[group.group_id stringValue];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
//捕获用户选择
Groups*group=[self.fetchedObjects objectAtIndex:indexath.row];
UITableViewCell*selectedCell=[tableView cellForRowAtIndexPath:indexPath];

NSString*selection=selectedCell.groupDescriptionLabel.text;//如果您知道CelforRowatineXpath将返回一种自定义单元格类型而不是通用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);
    //...
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath
{
//捕获用户选择
MyCellClass*selectedCell=(MyCellClass*)[tableView cellForRowAtIndexPath:indexPath];

NSString*selection=selectedCell.groupDescriptionLabel.text;//从视图中获取数据通常不是一个好主意。您不应该使用任何视图对象作为存储信息的方式

当您使用
cellforrowatinexpath
方法时,您已经获得了字符串

您应该能够在
didSelectRowAtIndexPath
中执行相同的操作,以获得相同的字符串

这样,您根本不必从标签中取出文本


谢谢

您不应该在视图中存储数据。您已经在
CellForRowatineXpath
中从数据源获取字符串。只需在
didSelectRow
代码中执行相同的操作即可获取字符串。@Fogmeister-我喜欢这个建议。谢谢!补充回答:D很高兴它能帮助您解决这个问题。我的方法有点老套因为我不知道如何从源代码中获取数据,但另一个建议给了我一个更简洁的方法。我感谢这个建议。+1直接回答了我的问题。虽然它回答了所问的问题,但使用视图元素存储信息通常不是一个好主意。@Fogmeister,很好。我应该这样做注意到.User55410,您应该在输入后立即从单元格中收集用户输入,并将其保存在模型中。尝试从tableView:DidSelectRowatineXpath:中的单元格字段中收集数据是错误的。如果用户将数据输入单元格,然后将该单元格从屏幕上滚动,则数据将丢失。这给了我一个提示艾德,谢谢。