UITableViewCell 在突出显示时使标签的背景清晰
我在 UITableViewCell 上有一个 UIlabel,它是我以编程方式创建的(即不是笔尖或子类)。
当单元格突出显示(变成蓝色)时,它会使 UILabels 的所有背景颜色变得清晰。我有 2 个 UILabel,我不希望出现这种情况。 目前我在 UILabel 后面使用 UIImageViews 使其看起来背景颜色没有改变。但这似乎是一种低效的方法。
当 UITableViewCell 突出显示时,如何阻止某些 UILabel 的背景颜色发生变化?
I have a UIlabel on a UITableViewCell, which I've created programmatically (i.e. not a nib or a subclass).
When the cell is highlighted (goes blue) it makes all the background colors of the UILabels turn clear. I have 2 UILabels where I don't want this to be the case.
Currently I'm using UIImageViews behind the UILabel's to make it look like the background color doesn't change. But this seems an inefficient way to do it.
How can i stop certain UILabel's background color changing when the UITableViewCell is highlighted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您需要子类化 UITableViewCell 并重写以下两个方法:
Objective-C:
Swift
You need to subclass UITableViewCell and override the following two methods:
Objective-C:
Swift
防止背景颜色在突出显示时发生变化的另一种方法是在标签的
layer
上设置backgroundColor
属性,而不是在标签本身上设置。...
图层
不受单元格突出显示或选择的影响。Another way to keep the background color from changing on highlight is to set the
backgroundColor
property on the label'slayer
instead of on the label itself....
The
layer
is not affected by cell highlighting or selection.或者,对您不想更改颜色的标签进行子类化:
然后使用 setPersistentBackgroundColor: 显式设置颜色一次。这将防止在不使用自定义显式背景颜色更改方法的情况下从任何地方更改背景颜色。
这样做的优点是还可以在转换过程中消除标签中的清晰背景。
Alternatively subclass the label you don't want to change color:
Then set the color once explicitly using setPersistentBackgroundColor:. This will prevent background color changes from anywhere without using your custom explicit background color change method.
This has the advantage of also eliminating clear background in label during transitions.
我可能错了,但我找不到直接覆盖 Swift 中现有的 getter/setter 的方法。根据 user479821 的答案,我找到了一种似乎能产生预期结果的解决方法。我添加了 IBDesignable/IBInspectable 注释,以防您使用故事板,它渲染的是编辑器中的最终颜色。
I may be mistaken, but I couldn't find away to directly override an existing getter/setter in Swift. Building off user479821's answer, I found a workaround that seems to produce the desired results. I added the IBDesignable/IBInspectable annotations in case you use storyboard, which render's the final color in the editor.
我遇到了同样的问题,我猜这是 UIKit 框架错误。感谢上帝,我找到了一个解决方法:顺序很重要!只需按照顺序进行操作即可:
我不知道为什么,但此顺序工作正常,其他顺序使其看起来好像 c.textLabel.backgroundColor 在取消选择某些单元格后被迫清除它们。
这不是随机行为,它发生在单元第一次被重用时,而不是在创建它们时,也不是在它们第二次重用时发生。通过解决方法,它总是工作得很好。
I got same problem and I guess it is sort of a UIKit framework bug. Thanks God I got a workaround: Order matters!!! Just follow the sequence:
I don't know why, but this sequence works fine and other order makes it appear as if c.textLabel.backgroundColor was forced to clearColor in some cells after deselecting them.
It was not a random behavior, it happened the first time the cells were reused, not when they were created, nor when they were secondly reused. With the workaround it works fine always.
将背景颜色设置为 label.layer.backgroundColor 可以解决此问题。
看起来单元格在突出显示时会改变 UILabel 的背景颜色。
Set background color to label.layer.backgroundColor can fix this.
It seems cell will change backgroundColor of UILabel when highlighting.
我只是在 UILabel 子类中创建一个标志,以在 awakeFromNib 或 init 之后禁用背景颜色更改。这使得故事板中设置的初始颜色生效。而且我们不必调用任何额外的方法。
I just create a flag in UILabel subclass to disable background color change after awakeFromNib or init. This allows the initial color set in storyboard to takes effect. And we don't have to call any extra methods.
在我的应用程序中,我必须将 uitableviewcell 选择样式更改为蓝色,因此我必须设置 uiimage 视图,当我单击该单元格时,该单元格会突出显示为 Imageview。当我返回视图时,我删除了单元格的突出显示。我无法清楚地理解你的问题。所以只要给出我的代码并尝试这个,
祝你好运
In my apps i had to change the uitableviewcell selection style blue color, so i had to set uiimage view, when i clicked the cell is highlighted as Imageview. And i removed the highlighted of the cell when i returned to the view. And i couldnt understand your problem clearly. So just given my code and Try this one,
Best Of Luck
我必须对 UItableView 进行子类化,创建一个我不想制作透明背景的视图标签列表,并重写 setHighlighted:animated: 方法,以便重置特定标签的背景颜色。冗长而忠实,如果我有 UItableViewCell 的实际类的源代码就好了。
I had to subclass the UItableView, create a list of tags of the view that I want to not make transparent background, and override the setHighlighted:animated: method so that it reset the specific labels background colour. longwinding and fidely, if only I had the source code t the UItableViewCell's actual class.
我无法得到工作中可接受的答案;并不是说我认为答案是错误的(远非如此——这似乎是正确的方法),而是当我编写我的第一个 iOS 应用程序时,我的头已经开始思考子类了。所以我决定作弊。
假设自定义单元格由
TableViewCell
类控制。我创建了一个具有正确背景颜色的单像素.png
文件,并在自定义单元格中创建了一个UIImageView
对象。我使用属性检查器设置默认图像并使用“缩放至填充”。为了更具视觉吸引力,我将以下内容添加到身份检查器中的用户定义的运行时属性中:瞧,便宜的圆角。
它直接位于 UILabel 后面,看起来很业务,尽管它显然不会随内容调整大小(因为唯一的内容是它自己的图像)。对我来说这不是问题,因为要显示的数据无论如何都必须是固定大小。
对于某些值,最好有不同的颜色,可以非常轻松地设置:
其中“cell”是
cellForRowAtIndexPath
方法中我的自定义单元格的化身,使用TableViewCell *cell 创建;
。没有数据可显示?我确信这是一个愚蠢的想法,这是有充分理由的,但由于我刚刚开始 iOS 开发,所以这是一个对我有用的简单解决方案。请随意告诉我为什么不这样做!
I couldn't get the accepted answer to work; not that I think the answer is incorrect (far from it - that appears to be the Right Way), but as I was writing my first iOS app my head was spinning with subclasses already. So I decided to cheat.
Let's say the custom cell was controlled by a
TableViewCell
class. I created a single pixel.png
file of the correct background colour and created aUIImageView
object in my custom cell. I used the Attributes inspector to set the default image and used 'Scale To Fill'. For a little more visual appeal, I then added the following to the User Defined Runtime Attributes in the Identity inspector:Voilá, rounded corners on the cheap.
Positioned directly behind the UILabel, it looks the business, though it obviously won't resize with content (as the only content is its own image). Not a problem for me as the data to be displayed had to be a fixed size anyway.
On some values it was good to have a different colour, which can be set really easily:
Where 'cell' is the incarnation of my custom cell in the
cellForRowAtIndexPath
method, created usingTableViewCell *cell;
. No data to display?I'm sure there will be a good reason why this is a daft idea, but as I'm just starting out with iOS development this was an easy fix that worked for me. Feel free to tell me why not to do it!