在didSelectRowAtIndexPath访问自定义标签属性 [英] Access custom label property at didSelectRowAtIndexPath

查看:105
本文介绍了在didSelectRowAtIndexPath访问自定义标签属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cellForRowAtIndexPath的每个单元格都有一个UILabel。

I have a UILabel for each cell at cellForRowAtIndexPath.

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;

我想使用indexpath在didSelectRowAtIndexPath访问该字符串myString。

I want to access that string "myString" at didSelectRowAtIndexPath using indexpath.

NSString *anotherString = cell.textLabel.text;

返回null。

现在如果在cellForRowAtIndexPath,我执行了类似 cell.textLabel.text = theString; 的操作,然后didSelectRowAtIndexPath返回相应的单元格。

Now if at cellForRowAtIndexPath, I did something like cell.textLabel.text = theString; then the didSelectRowAtIndexPath returns the appropriate cell.

我的问题是,如何在doSelectRowAtIndexPath中访问我应用于单元格的UILabel中的文本?

My question is, how can I access the text in the UILabel I apply to the cell, at didSelectRowAtIndexPath?

此外,在didSelectRowAtIndexPath中记录单元格将返回 cell:< UITableViewCell:0x5dcb9d0; frame =(0 44; 320 44); autoresize = W; layer =< CALayer:0x5dbe670>>

Also, logging the cell in didSelectRowAtIndexPath returns cell: <UITableViewCell: 0x5dcb9d0; frame = (0 44; 320 44); autoresize = W; layer = <CALayer: 0x5dbe670>>

编辑:

    NSString *myString = [[results objectAtIndex:indexPath.row] valueForKey:@"name"];
//cell.textLabel.text = myString;

CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = 20;
CGRect frame = CGRectMake(10.0f, 10.0f, width, height);

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];
[cell.contentView addSubview:cellLabel];
[cellLabel release];

return cell;


推荐答案

在这行代码中:

NSString *anotherString = cell.textLabel.text;

你是如何获得这个细胞的?它没有?此外,您正在访问的 textLabel 字段是UITableViewCell中的默认标签,而不是您在 -cellForRowAtIndexPath 中添加的标签。以下是如何从 -didSelectRowAtIndexPath 获取单元格:

How are you obtaining the cell? Is it nil? Also, the textLabel field you're accessing is the default label in the a UITableViewCell and not the label you are adding in -cellForRowAtIndexPath. Here is how you can get the cell from -didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tv 
                 didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];

}

此时的问题是,你可以不按名称访问UILabel,但是,如果您设置了标记,则可以访问它。因此,当您创建UILabel时,请设置如下标记:

The issue at this point, though, is that you can't access the UILabel by name, however, you can access it if you've set a tag. So, when you create your UILabel, set the tag like this:

UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.text = myString;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor whiteColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:14.0f];

// Set the tag to any integer you want
cellLabel.tag = 100;

[cell.contentView addSubview:cellLabel];
[cellLabel release];

所以,现在你可以通过标签访问UILabel:

So, now you can access the UILabel by tag:

- (void)tableView:(UITableView *)tv 
          didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tv cellForRowAtIndexPath:indexPath];

    UILabel *label = (UILabel*)[cell viewWithTag:100];

    NSLog(@"Label Text: %@", [label text]);
}

这篇关于在didSelectRowAtIndexPath访问自定义标签属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆