标签在我的可重复使用的表格单元中徘徊 [英] Labels are lingering in my reusable table cells

查看:76
本文介绍了标签在我的可重复使用的表格单元中徘徊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其单元格包含标签。每当我将可重复使用的单元移出队列时,旧标签仍在其上徘徊。我可以使用以下方法删除它们:

I have a table whose cells contain labels. Whenever I dequeue a reusable cell, the old labels are still lingering on it. I was able to remove them with this:

    for(int a=[[newcell subviews]count]-1; a>=0;a--)
    {
        if([[[[newcell subviews]objectAtIndex:a]class] isSubclassOfClass:[UILabel class]])
        {
            [[[newcell subviews] objectAtIndex:a] removeFromSuperview];
        }
    }

但是当我选择单元格时,我可以看到在新顶部的旧文本。我试过了:

But when I select the cell, I can see the old text on top of the new. I tried this:

    [[newcell.selectedBackgroundView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];
    [[newcell.backgroundView subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)];

但这没有用。如何使旧标签从所选单元格以及该单元格的常规视图中消失?

But it didn't work. How can I make the old labels disappear from the selected cell as well as the regular view of the cell?

推荐答案

当您在 cellForRowAtIndexPath:中的单元格中添加子视图时,无论该问题是要出队还是新创建,都容易出现问题。结果,每次行被重用时,您最终都会创建一个新的子视图,并且旧的子视图会累积。

This kind of problem tends to happen when you add subviews to your cells in cellForRowAtIndexPath: regardless of whether it's being dequeued or newly created. As a result, you end up creating a new subview each time the row is reused, and the old subviews accumulate.

您要做的是使用相同的子视图。每次子视图,但每次都设置相关属性(例如标签或颜色)。查看的答案。如何清除单元重用时,它会完全消失吗?看看一些可能的方法。

What you instead want to do is to use the same subview each time, but just set the relevant attributes (e.g., labels or color) each time. Check out the answers to How do I clear a cell completely when I reuse it? to see some possible approaches.

这篇关于标签在我的可重复使用的表格单元中徘徊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆