3

我是 iPhone devlopemnt 的新手,我正在开发一个应用程序。在该应用程序中,我需要在UITableView. 为了解决这个问题,我遵循一些概念,例如动态创建标签UITableViewCell以显示商品及其价格。

在应用程序中,第一个indexid == 1意味着我在单击列表上的特定单元格时显示列表位置,我正在获取最喜欢的项目列表,其价格意味着 indexid = 2 ...单击后退按钮 indexid = 1 然后我需要隐藏该标签.. .但标签没有隐藏它显示标价而不是项目列表

lblText.text = nil;
btnBack.hidden = FALSE;
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{     
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
     cell.textLabel.highlightedTextColor = [UIColor orangeColor];

}

    /////////////////////   Cell Title    /////////////////////
    if (indexId == 1) 
    {
//        NSLog(@"%@",lblText1);
       cell.textLabel.text = [NSString stringWithFormat:@"%@", [test.arrTitle objectAtIndex:indexPath.row]];
        lblText = [[UILabel alloc] initWithFrame:CGRectMake(250, 7, 40, 30)]; // For right alignment
        lblText.text = [test.arrId objectAtIndex:indexPath.row];
        [lblText setTextAlignment:UITextAlignmentCenter];
        lblText.textColor = [UIColor redColor];
        lblText.backgroundColor = [UIColor clearColor];
        [cell addSubview:lblText];

       cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"products-category-bg.png"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [lblText release];

    }
    else
    {
        lblText.hidden = YES;
        cell.textLabel.text = [NSString stringWithFormat:@"  %@", [test.arrTitle objectAtIndex:indexPath.row]];        

            cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"product-bg-hover.png"]];
    }       
    return cell;
}

我有以下问题。标签创建正常,但单击后退按钮后,我在主表视图中获得标签值。

如何释放带有数据的标签对象?

感谢和问候

请帮我解决这个问题

4

3 回答 3

0

如果您对应用程序中的不同状态使用相同的表视图实例,那么您也将重复使用之前创建的单元格:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

对于每个单元格,当indexid == 1您也这样做时:

[cell addSubview:lblText];

对我来说,这表示您创建的每个单元格(并且将被重复使用)都有一个UILabel添加为子视图。要真正重用该单元格,您需要UILabel在逻辑表明您需要再次添加它之前将其删除。这不仅会在您不需要时删除单元格,还会阻止您在运行时向单元格添加额外的 UILabel 实例。在检查之前尝试添加下面的代码indexid

if (cell == nil)
{     
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0];
     cell.textLabel.highlightedTextColor = [UIColor orangeColor];
}

// Be sure to remove the UILabels on the re-used cell.
for(UIView *view in cell.subviews)
    if ([view isKindOfClass:([UILabel class])])
        [view removeFromSuperview];

if (indexid == 1) { ....

这将在每次创建单元格时删除 UILabel,允许您有条件地将其重新添加为子视图。

于 2013-03-14T14:49:48.970 回答
0

尝试为您的标签设置标签(将帮助您将数据设置为标签,您可以通过调用获取标签

[单元格viewWithTag:theLabelTag]

其中theLabelTag标签可以是索引)并将标签值设置为

tableView: willDisplayCell: forRowAtIndexPath:

代替

表视图:cellForRowAtIndexPath:

于 2013-03-14T14:59:36.920 回答
0

您的代码对我来说看起来不错,只需将上面代码中的一行替换为下面写的链接

[cell.contentView addSubview:lblText];

反而

[cell addSubview:lblText];

希望它能解决您的问题。

于 2012-12-29T12:05:42.147 回答