41

我在 UITableViewCell 上有一个 UIlabel,它是我以编程方式创建的(即不是笔尖或子类)。

当单元格突出显示(变为蓝色)时,它会使 UILabel 的所有背景颜色变得清晰。我有 2 个 UILabel,我不希望出现这种情况。目前我正在使用 UILabel 后面的 UIImageViews 使其看起来背景颜色没有改变。但这似乎是一种低效的方法。

当 UITableViewCell 突出显示时,如何停止某些 UILabel 的背景颜色变化?

4

10 回答 10

55

您需要继承 UITableViewCell 并覆盖以下两个方法:

目标-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;
}

迅速

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
}
于 2014-03-02T14:36:52.607 回答
46

另一种防止背景颜色在高亮时改变的方法是在标签上而不是在标签本身上设置backgroundColor属性。layer

#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;
}

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

于 2014-02-18T21:45:54.037 回答
41

或者子类您不想更改颜色的标签:

@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: 显式设置一次颜色。这将防止在不使用自定义显式背景颜色更改方法的情况下从任何地方更改背景颜色。

这还具有在转换期间消除标签中清晰背景的优点。

于 2010-10-18T21:49:23.527 回答
5

我可能弄错了,但我找不到直接覆盖 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
        }
    }
    }
}
于 2015-03-31T03:43:15.627 回答
3

我遇到了同样的问题,我想这是一个 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 在取消选择某些单元格后被迫清除颜色。

这不是随机行为,它发生在第一次重用单元格时,不是在创建它们时,也不是在第二次重用时。使用解决方法,它始终可以正常工作。

于 2011-12-06T00:28:43.583 回答
2

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

突出显示时,单元格似乎会改变 UILabel 的 backgroundColor 。

于 2015-07-03T09:20:29.250 回答
1

我只是在 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
于 2014-02-05T18:51:50.253 回答
0

在我的应用程序中,我必须更改 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];

   }

祝你好运

于 2010-06-03T11:06:02.743 回答
0

我必须对 UItableView 进行子类化,创建一个我不想制作透明背景的视图标签列表,并覆盖 setHighlighted:animated: 方法,以便它重置特定标签的背景颜色。冗长而忠实,如果我有 UItableViewCell 的实际类的源代码就好了。

于 2010-06-03T14:04:39.200 回答
-1

我无法得到公认的工作答案;并不是说我认为答案不正确(远非如此——这似乎是正确的方法),但当我编写我的第一个 iOS 应用程序时,我的头脑已经在与子类一起旋转。所以我决定作弊。

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

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

瞧,便宜的圆角。

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

在某些值上,最好有不同的颜色,可以很容易地设置:

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

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

cell.deviceDetailBackground.image = nil;

我确信这是一个愚蠢的想法会有充分的理由,但由于我刚刚开始进行 iOS 开发,这是一个对我有用的简单修复。随时告诉我为什么不这样做!

于 2013-03-18T15:12:15.087 回答