UITableViewCell 中的 UILabel backgroundColor 始终恢复为初始值
在我的应用程序中,我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些基于状态的属性是由表视图设置的;我相信背景颜色就是其中之一。换句话说,表格视图正在更改
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 ofdetailTextLabel
in this method your problem will go away.当 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.
是的..也许您没有在 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.
我解决这个问题的方法是创建一个名为 HighlightedLabel 的 UILabel 子类,它具有以下初始化程序:
然后,当我分配此单元格时,我指定突出显示和非突出显示的背景颜色。
这非常有效 - 当我选择单元格时,颜色就是我想要的。
The way that I fixed this was to create a UILabel subclass called HighlightedLabel which has the following initialiser:
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.