如何在UITableViewCells中清除UILabel?

时间:2011-04-07 05:02:29

标签: iphone uitableview uilabel

我在UITableView中的每个单元格中放置了多个UILabel,而不是单个cell.textLabel.text。然后我使用reloaddata来添加新的uilabels。我如何摆脱旧标签?

编辑:如果我在一个单元格中放入5个标签,然后仅使用2个标签重新加载单元格,那么从上次调用cellForRowAtIndexPath时还剩下3个标签。 如果我像Goldeen所说的那样使用viewWithTag,我可以重复使用旧标签但是我可以从内存中删除我不想要的标签吗?

修改 这是我的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell";

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UILabel *label =  [[[UILabel alloc] initWithFrame:CGRectMake(j*50.0, 0, 49.0,logicTable.rowHeight)] autorelease];
label.tag = 1;
label.text = [NSString stringWithFormat:@"ABC"];
label.textAlignment = UITextAlignmentCenter; 
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
UIViewAutoresizingFlexibleHeight;

[cell.contentView addSubview:label];

return cell; 

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(j*50.0, 0, 49.0,logicTable.rowHeight)] autorelease]; label.tag = 1; label.text = [NSString stringWithFormat:@"ABC"]; label.textAlignment = UITextAlignmentCenter; label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight; [cell.contentView addSubview:label]; return cell;

1 个答案:

答案 0 :(得分:3)

你正在做的是,在你的cellForRowAtIndexPath方法中,你正在设置你的UITableViewCells中有一些标签,每次,你都是从头开始制作标签。您应该做的是,如果要创建新单元格,请设置标签,然后在此外的标签上设置值,以充分利用重用表格视图单元格的功能,以提高滚动表格视图的性能。 / p>

关键方法是-viewWithTag:,它与UIView上的tag属性一起用于查找特定的子视图。

一些示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"MyCellIdentifier";

    UITableViewCell *cell = (WHArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *firstLabel = nil;
    UILabel *secondLabel = nil;
    UILabel *thirdLabel = nil;
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        firstLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0.0, 0.0, 20.0, 20.0)] autorelease];
        firstLabel.tag = 1;
        [cell addSubview:firstLabel];

        secondLabel = [[[UILabel alloc] initWithFrame: CGRectMake(20.0, 0.0, 20.0, 20.0)] autorelease];
        secondLabel.tag = 2;
        [cell addSubview:secondLabel];

        thirdLabel = [[[UILabel alloc] initWithFrame: CGRectMake(40.0, 0.0, 20.0, 20.0)] autorelease];
        thirdLabel.tag = 3;
        [cell addSubview:thirdLabel];
    }    
    else
    {
        firstLabel = (UILabel *)[cell viewWithTag:1];
        secondLabel = (UILabel *)[cell viewWithTag:2];
        thirdLabel = (UILabel *)[cell viewWithTag:3];
    }
    firstLabel.text = @"First Label's Text Here";
    secondLabel.text = @"Second Label's Text Here";
    thirdLabel.text = @"Third Label's Text Here";
    return cell;
}