如何清除 UITableViewCells 中的 UILabels?

发布于 2024-10-30 21:38:44 字数 1171 浏览 0 评论 0原文

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

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

编辑: 这是我的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 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; 

}

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] 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; 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漆黑的白昼 2024-11-06 21:38:44

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

关键方法是 -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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文