Jon*_*an. 41 iphone objective-c background-color uitableview uilabel
我在UITableViewCell上有一个UIlabel,我是以编程方式创建的(即不是nib或子类).
当单元格突出显示(变为蓝色)时,它会使UILabels的所有背景颜色变为清晰.我有2个UILabels,我不希望这样.目前我在UILabel后面使用UIImageViews使它看起来像背景颜色不会改变.但这似乎是一种效率低下的方法.
当UITableViewCell突出显示时,如何停止某些UILabel的背景颜色变化?
Vah*_*han 54
您需要继承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;
}
迅速
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
}
Tom*_*mmy 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不影响细胞高亮或选择.
小智 40
或者,为您不想更改颜色的标签创建子类:
@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:显式设置颜色:这样可以防止在任何地方更改背景颜色,而无需使用自定义显式背景颜色更改方法
这具有在转换期间也消除标签中的清晰背景的优点.
小智 5
我可能弄错了,但我找不到直接覆盖Swift中现有的getter/setter.基于user479821的答案,我发现了一种似乎可以产生预期结果的解决方法.我添加了IBDesignable/IBInspectable注释,以防您使用storyboard,它在编辑器中呈现最终颜色.
@IBDesignable
class PersistentBackgroundLabel: UILabel {
    @IBInspectable var persistentBackgroundColor: UIColor = UIColor.clearColor() {
    didSet {
        super.backgroundColor = persistentBackgroundColor
    }
    }
    override var backgroundColor: UIColor? {
    didSet {
        if backgroundColor != persistentBackgroundColor {
            backgroundColor = persistentBackgroundColor
        }
    }
    }
}
我必须对 UItableView 进行子类化,创建一个我不想制作透明背景的视图标签列表,并重写 setHighlighted:animated: 方法,以便重置特定标签的背景颜色。冗长而忠实,如果我有 UItableViewCell 的实际类的源代码就好了。
| 归档时间: | 
 | 
| 查看次数: | 18212 次 | 
| 最近记录: |