UITableViewCell 中的 UILabel backgroundColor 始终恢复为初始值

发布于 2024-09-26 08:43:49 字数 349 浏览 10 评论 0原文

在我的应用程序中,我有一个 UITableViewCell 用于显示背景颜色设置。在detailTextLabel中,它显示颜色的名称,背景设置为实际颜色,例如[UIColoryanColor]。请注意,我仅设置detailTextLabel 的背景,而不是整个UITableViewCell。当用户点击单元格时,他们将被带到另一个 UITableView,让他们选择颜色,当他们返回到前一个 UITableView 时,UILabel 的背景颜色将更新为新颜色。

问题是,每当我返回到初始 UITableView 时,UILabel 的背景颜色会立即更新,然后返回到初始颜色。我不明白为什么它会恢复。有什么建议吗?

谢谢你!

In my app, I have a UITableViewCell which is used to display a background color setting. In the detailTextLabel, it displays the name of the color with the background set to the actual color, e.g. [UIColor cyanColor]. Note that I am setting the background of the detailTextLabel only, not the whole UITableViewCell. When users tap on the cell they are taken to another UITableView which lets them choose a color, and when they return to the previous UITableView the backgroundColor of the UILabel is updated to the new color.

The problem is, whenever I return to the initial UITableView, the UILabel's backgroundColor updates momentarily and then returns to the initial color. I cannot find out why it would be reverting. Any suggestions?

Thank you!

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

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

发布评论

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

评论(4

羁客 2024-10-03 08:43:49

一些基于状态的属性是由表视图设置的;我相信背景颜色就是其中之一。换句话说,表格视图正在更改 detailTextLabel 的背景颜色,可能是取消突出显示所选内容的一部分。

表视图设置基于状态的属性后,表委托将有最后一次机会更新每个单元格的外观。这是在委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中完成的。也许如果您在此方法中设置 detailTextLabel 的背景颜色,您的问题就会消失。

Some state-based properties are set by the table view; I believe that background color is one of them. In other words, the table view is changing the background color of detailTextLabel, probably as part of unhighlighting the selection.

After the table view sets state-based properties, the table delegate is given a final chance to update the appearance of each cell. This is done in the delegate's tableView:willDisplayCell:forRowAtIndexPath: method. Perhaps if you set the background color of detailTextLabel in this method your problem will go away.

Spring初心 2024-10-03 08:43:49

当 cellForRowAtIndexPath 执行时,它通常会创建并返回一个新单元格。

从您的问题来看,尚不清楚您是否正在重新创建细胞,但如果是,这可以解释您所描述的行为。

When cellForRowAtIndexPath executes, it typically creates and returns a new cell.

From your question, it is unclear if you are recreating the cell or not, but if you are, this could explain the behavior you describe.

灯下孤影 2024-10-03 08:43:49

是的..也许您没有在 cellForRowAtIndexPath 方法中重复使用单元格。
如果是,请尝试重复使用您的细胞,而不是每次都创建新细胞。

Yes..Maybe you are not re-using your cells in cellForRowAtIndexPath methode.
If it is, try to re-use your cells rather than creating new one everytime.

り繁华旳梦境 2024-10-03 08:43:49

我解决这个问题的方法是创建一个名为 HighlightedLabel 的 UILabel 子类,它具有以下初始化程序:

- (id)initWithHighlightedBackgroundColor:(UIColor *)highlightedBackgroundColor nonHiglightedBackgroundColor:(UIColor *)nonHighlightedBackgroundColor
    {
        self = [super init];
        if(self)
        {
            _highlightedBackgroundColor = highlightedBackgroundColor;
            _nonHighlightedBackgroundColor = nonHighlightedBackgroundColor;
            self.backgroundColor = nonHighlightedBackgroundColor;
        }
        return self;
    }


    -(void)setHighlighted:(BOOL)highlighted
    {
        if(highlighted)
        {
            self.backgroundColor = self.highlightedBackgroundColor;
        }
        else
        {
            self.backgroundColor = self.nonHighlightedBackgroundColor;
        }
    }

然后,当我分配此单元格时,我指定突出显示和非突出显示的背景颜色。

这非常有效 - 当我选择单元格时,颜色就是我想要的。

The way that I fixed this was to create a UILabel subclass called HighlightedLabel which has the following initialiser:

- (id)initWithHighlightedBackgroundColor:(UIColor *)highlightedBackgroundColor nonHiglightedBackgroundColor:(UIColor *)nonHighlightedBackgroundColor
    {
        self = [super init];
        if(self)
        {
            _highlightedBackgroundColor = highlightedBackgroundColor;
            _nonHighlightedBackgroundColor = nonHighlightedBackgroundColor;
            self.backgroundColor = nonHighlightedBackgroundColor;
        }
        return self;
    }


    -(void)setHighlighted:(BOOL)highlighted
    {
        if(highlighted)
        {
            self.backgroundColor = self.highlightedBackgroundColor;
        }
        else
        {
            self.backgroundColor = self.nonHighlightedBackgroundColor;
        }
    }

Then when I allocate this cell I specify the highlighted and non-highlighted background colour.

This works perfectly - when I select the cell the colour is what I want.

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