UITableViewCell 在突出显示时使标签的背景清晰

发布于 2024-09-04 05:28:31 字数 255 浏览 12 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(10

╭ゆ眷念 2024-09-11 05:28:31

您需要子类化 UITableViewCell 并重写以下两个方法:

Objective-C:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setSelected:selected animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

Swift

override func setSelected(_ selected: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setSelected(selected, animated: animated)
    myLabel.backgroundColor = color
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    myLabel.backgroundColor = color
}

You need to subclass UITableViewCell and override the following two methods:

Objective-C:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIColor *backgroundColor = self.myLabel.backgroundColor;
    [super setSelected:selected animated:animated];
    self.myLabel.backgroundColor = backgroundColor;
}

Swift

override func setSelected(_ selected: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setSelected(selected, animated: animated)
    myLabel.backgroundColor = color
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    myLabel.backgroundColor = color
}
才能让你更想念 2024-09-11 05:28:31

防止背景颜色在突出显示时发生变化的另一种方法是在标签的 layer 上设置 backgroundColor 属性,而不是在标签本身上设置。

#import <QuartzCore/QuartzCore.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Get a table cell
    UITableViewCell *cell = [tableView dequeueReusableCellForIdentifier:@"cell"];

    // Set up the table cell
    cell.textLabel.text = @"This is a table cell.";

    // If you're targeting iOS 6, set the label's background color to clear
    // This must be done BEFORE changing the layer's backgroundColor
    cell.textLabel.backgroundColor = [UIColor clearColor];

    // Set layer background color
    cell.textLabel.layer.backgroundColor = [UIColor blueColor].CGColor;

    return cell;
}

图层不受单元格突出显示或选择的影响。

Another way to keep the background color from changing on highlight is to set the backgroundColor property on the label's layer instead of on the label itself.

#import <QuartzCore/QuartzCore.h>

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Get a table cell
    UITableViewCell *cell = [tableView dequeueReusableCellForIdentifier:@"cell"];

    // Set up the table cell
    cell.textLabel.text = @"This is a table cell.";

    // If you're targeting iOS 6, set the label's background color to clear
    // This must be done BEFORE changing the layer's backgroundColor
    cell.textLabel.backgroundColor = [UIColor clearColor];

    // Set layer background color
    cell.textLabel.layer.backgroundColor = [UIColor blueColor].CGColor;

    return cell;
}

The layer is not affected by cell highlighting or selection.

抚你发端 2024-09-11 05:28:31

或者,对您不想更改颜色的标签进行子类化:

@interface PersistentBackgroundLabel : UILabel {
}

- (void)setPersistentBackgroundColor:(UIColor*)color;

@end


@implementation PersistentBackgroundLabel

- (void)setPersistentBackgroundColor:(UIColor*)color {
    super.backgroundColor = color;
}

- (void)setBackgroundColor:(UIColor *)color {
    // do nothing - background color never changes
}

@end

然后使用 setPersistentBackgroundColor: 显式设置颜色一次。这将防止在不使用自定义显式背景颜色更改方法的情况下从任何地方更改背景颜色。

这样做的优点是还可以在转换过程中消除标签中的清晰背景。

Alternatively subclass the label you don't want to change color:

@interface PersistentBackgroundLabel : UILabel {
}

- (void)setPersistentBackgroundColor:(UIColor*)color;

@end


@implementation PersistentBackgroundLabel

- (void)setPersistentBackgroundColor:(UIColor*)color {
    super.backgroundColor = color;
}

- (void)setBackgroundColor:(UIColor *)color {
    // do nothing - background color never changes
}

@end

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.

绻影浮沉 2024-09-11 05:28:31

我可能错了,但我找不到直接覆盖 Swift 中现有的 getter/setter 的方法。根据 user479821 的答案,我找到了一种似乎能产生预期结果的解决方法。我添加了 IBDesignable/IBInspectable 注释,以防您使用故事板,它渲染的是编辑器中的最终颜色。

@IBDesignable
class PersistentBackgroundLabel: UILabel {
    @IBInspectable var persistentBackgroundColor: UIColor = UIColor.clearColor() {
    didSet {
        super.backgroundColor = persistentBackgroundColor
    }
    }

    override var backgroundColor: UIColor? {
    didSet {
        if backgroundColor != persistentBackgroundColor {
            backgroundColor = persistentBackgroundColor
        }
    }
    }
}

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.

@IBDesignable
class PersistentBackgroundLabel: UILabel {
    @IBInspectable var persistentBackgroundColor: UIColor = UIColor.clearColor() {
    didSet {
        super.backgroundColor = persistentBackgroundColor
    }
    }

    override var backgroundColor: UIColor? {
    didSet {
        if backgroundColor != persistentBackgroundColor {
            backgroundColor = persistentBackgroundColor
        }
    }
    }
}
已下线请稍等 2024-09-11 05:28:31

我遇到了同样的问题,我猜这是 UIKit 框架错误。感谢上帝,我找到了一个解决方法:顺序很重要!只需按照顺序进行操作即可:

- (void)tableView:(UITableView *)t willDisplayCell:(UITableViewCell*)c forRowAtIndexPath:(NSIndexPath *)i {
UIColor *bgc = [UIColor redColor];
// Set up the cell in following order:
c.textLabel.backgroundColor = bgc;
c.backgroundColor = bgc;
c.textLabel.text = @"foo";
}

我不知道为什么,但此顺序工作正常,其他顺序使其看起来好像 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:

- (void)tableView:(UITableView *)t willDisplayCell:(UITableViewCell*)c forRowAtIndexPath:(NSIndexPath *)i {
UIColor *bgc = [UIColor redColor];
// Set up the cell in following order:
c.textLabel.backgroundColor = bgc;
c.backgroundColor = bgc;
c.textLabel.text = @"foo";
}

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.

小伙你站住 2024-09-11 05:28:31

将背景颜色设置为 label.layer.backgroundColor 可以解决此问题。

看起来单元格在突出显示时会改变 UILabel 的背景颜色。

Set background color to label.layer.backgroundColor can fix this.

It seems cell will change backgroundColor of UILabel when highlighting.

喜爱纠缠 2024-09-11 05:28:31

我只是在 UILabel 子类中创建一个标志,以在 awakeFromNib 或 init 之后禁用背景颜色更改。这使得故事板中设置的初始颜色生效。而且我们不必调用任何额外的方法。

@implementation MWPersistentBGLabel {
    BOOL disableBackgroundColorChange;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}

- (void)setup {
    // disable background color change after setup is called
    disableBackgroundColorChange = YES;
}

// if we ever have to change the color later on, we can do so with this method
- (void)setPersistentBackgroundColor:(UIColor*)color {
    [super setBackgroundColor:color];
}

- (void)setBackgroundColor:(UIColor *)color {
    if (!disableBackgroundColorChange) {
        [super setBackgroundColor:color];
    }
    // do nothing - background color never changes
}

@end

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.

@implementation MWPersistentBGLabel {
    BOOL disableBackgroundColorChange;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}

- (void)setup {
    // disable background color change after setup is called
    disableBackgroundColorChange = YES;
}

// if we ever have to change the color later on, we can do so with this method
- (void)setPersistentBackgroundColor:(UIColor*)color {
    [super setBackgroundColor:color];
}

- (void)setBackgroundColor:(UIColor *)color {
    if (!disableBackgroundColorChange) {
        [super setBackgroundColor:color];
    }
    // do nothing - background color never changes
}

@end
旧时模样 2024-09-11 05:28:31

在我的应用程序中,我必须将 uitableviewcell 选择样式更改为蓝色,因此我必须设置 uiimage 视图,当我单击该单元格时,该单元格会突出显示为 Imageview。当我返回视图时,我删除了单元格的突出显示。我无法清楚地理解你的问题。所以只要给出我的代码并尝试这个,

   cellForRowAtIndexPath Method:

CGRect a=CGRectMake(0, 0, 300, 100);
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
bImg.image=[UIImage imageNamed:@"bg2.png"]; 
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.selectedBackgroundView=bImg;
[bImg release];


   -(void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:YES];

  NSIndexPath *deselect = [self.tableView indexPathForSelectedRow];
  [self.tableView deselectRowAtIndexPath:deselect animated:NO];

   }

祝你好运

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,

   cellForRowAtIndexPath Method:

CGRect a=CGRectMake(0, 0, 300, 100);
UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
bImg.image=[UIImage imageNamed:@"bg2.png"]; 
[bImg setContentMode:UIViewContentModeScaleToFill];
cell.selectedBackgroundView=bImg;
[bImg release];


   -(void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:YES];

  NSIndexPath *deselect = [self.tableView indexPathForSelectedRow];
  [self.tableView deselectRowAtIndexPath:deselect animated:NO];

   }

Best Of Luck

过气美图社 2024-09-11 05:28:31

我必须对 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.

横笛休吹塞上声 2024-09-11 05:28:31

我无法得到工作中可接受的答案;并不是说我认为答案是错误的(远非如此——这似乎是正确的方法),而是当我编写我的第一个 iOS 应用程序时,我的头已经开始思考子类了。所以我决定作弊。

假设自定义单元格由 TableViewCell 类控制。我创建了一个具有正确背景颜色的单像素 .png 文件,并在自定义单元格中创建了一个 UIImageView 对象。我使用属性检查器设置默认图像并使用“缩放至填充”。为了更具视觉吸引力,我将以下内容添加到身份检查器中的用户定义的运行时属性中:

KeyPath                Type         Value
layer.cornerRadius     Number       4
layer.masksToBounds    Boolean      YES

瞧,便宜的圆角。

它直接位于 UILabel 后面,看起来很业务,尽管它显然不会随内容调整大小(因为唯一的内容是它自己的图像)。对我来说这不是问题,因为要显示的数据无论如何都必须是固定大小。

对于某些值,最好有不同的颜色,可以非常轻松地设置:

cell.deviceDetailBackground.image = [UIImage imageNamed:@"detailBackground2.png"];

其中“cell”是 cellForRowAtIndexPath 方法中我的自定义单元格的化身,使用 TableViewCell *cell 创建;。没有数据可显示?

cell.deviceDetailBackground.image = nil;

我确信这是一个愚蠢的想法,这是有充分理由的,但由于我刚刚开始 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 a UIImageView 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:

KeyPath                Type         Value
layer.cornerRadius     Number       4
layer.masksToBounds    Boolean      YES

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:

cell.deviceDetailBackground.image = [UIImage imageNamed:@"detailBackground2.png"];

Where 'cell' is the incarnation of my custom cell in the cellForRowAtIndexPath method, created using TableViewCell *cell;. No data to display?

cell.deviceDetailBackground.image = nil;

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!

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