更改uitableviewcell子视图UILabel文本

问题描述

| 这是我的tableviewcell的设置方式
else if(indexPath.row == 3)
{
    cell.textLabel.text = @\"Category\";

    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150,2,145,34)];
    categoryLabel.adjustsFontSizetoFitWidth = YES;
    categoryLabel.textColor = [UIColor blackColor];
    categoryLabel.font = [UIFont systemFontOfSize:17.0];
    categoryLabel.text = @\"select a category\";
    categoryLabel.backgroundColor = [UIColor clearColor];
    categoryLabel.textAlignment = UITextAlignmentRight;
    categoryLabel.tag = 3;
    [cell addSubview:categoryLabel];
    [categoryLabel release];
}
稍后,我需要在程序中更改类别标签的文本。 我该如何完成?我假设我需要使用标记来引用UILabel?     

解决方法

这是我的tableviewcell的设置方式
else if(indexPath.row == 3)
{
    cell.textLabel.text = @\"Category\";

    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150,2,145,34)];
    categoryLabel.adjustsFontSizeToFitWidth = YES;
    categoryLabel.textColor = [UIColor blackColor];
    categoryLabel.font = [UIFont systemFontOfSize:17.0];
    categoryLabel.text = @\"select a category\";
    categoryLabel.backgroundColor = [UIColor clearColor];
    categoryLabel.textAlignment = UITextAlignmentRight;
    categoryLabel.tag = 3;
    [cell addSubview:categoryLabel];
    [categoryLabel release];
}
稍后,我需要在程序中更改类别标签的文本。我该如何完成?我假设我需要使用标记来引用UILabel? ======================== 我想到了... 我将UILabel放在头文件中,并更改了代码,如下所示
else if(indexPath.row == 3)
{
    cell.textLabel.text = @\"Category\";

    categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(150,34)];
    categoryLabel.adjustsFontSizeToFitWidth = YES;
    categoryLabel.textColor = [UIColor blackColor];
    categoryLabel.font = [UIFont systemFontOfSize:17.0];
    categoryLabel.text = [NSString stringWithFormat:@\"%@\",categoryFriendlyString];
    categoryLabel.backgroundColor = [UIColor clearColor];
    categoryLabel.textAlignment = UITextAlignmentRight;
    categoryLabel.tag = 3;
    [cell addSubview:categoryLabel];
}
然后,当我想稍后更改它时
categoryLabel.text = @\"sample string\";
最后在我的dealloc方法中发布了它。