开发者

How to clear UILabels inside UITableViewCells?

I put multiple UILabels inside every cell in a UITableView instead of a single cell.textLabel.text. I then use reloaddata to put new uilabels. How do i get rid of the old labels ?

edit: If i put 5 labels in a cell then reload the cell using only 2 labels, there are 3 more labels left over from the last time i called cellForRowAtIndexPath. If i use viewWithTag like Goldeen said, i can reuse old labels but can i remove labels i dont want from memory ?

edit: this is my method

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

MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[MyTableCell alloc] init开发者_高级运维WithStyle: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; 

}


What it sounds like you are doing is, in your cellForRowAtIndexPath method, you are setting up your UITableViewCells with some labels in them and each time, you are making the labels from scratch. What you should be doing is, setting up the labels if you are making a new cell, and then setting the values on the labels outside of this to fully utilize the ability to reuse table view cells to improve performance of scrolling the table view.

The key method is -viewWithTag: which, along with the tag property on UIView you can use to find a specific subview.

A little sample code:

- (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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜