9

I have some UILabel inside a UITableViewCell with opaque background color and white text. When this cell is selected, the UILabel's bg color seems to be obscured by the blue bg color of the selected cell. The UILabel text, however, is showing fine. How do I get the background color of the UILabel to show on top of the cell selection color?

enter image description here

pixelfreak
  • 17,714
  • 12
  • 90
  • 109
  • What you want do? explain properly. – Rakesh Bhatt Jul 04 '11 at 07:19
  • Did you find out solution for this ? I have similar problem. Let me know. I appreciate your help. – Deeps Oct 11 '11 at 14:02
  • @Deeps, I ended up not relying on the built-in selected style. What I did was subclass `UITableCell`, then override `setSelected` and show/hide a `UIView` that acts as my background. – pixelfreak Oct 12 '11 at 16:41

11 Answers11

16

Had this same issue in an multiple selection table view. Try setting the background color for those labels in -layoutSubviews. Worked for me.

jdrama418
  • 216
  • 1
  • 5
5

Looks like the background color of labels is changed together with the background color of a row (animated). I just subclassed the UILabel and "protected" backgroundColor changes after I set the initial color:

@interface MyLabel : UILabel {
    BOOL isColorLocked;
}
@property (assign, nonatomic) BOOL isColorLocked;
@end

@implementation MyLabel
@synthesize isColorLocked;
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (!isColorLocked) {
        super.backgroundColor = backgroundColor;
    }
}
@end
lexa
  • 414
  • 4
  • 5
1
@implementation CustomCell

//这样就行了啊。。

-(NSArray*)subviews{

    return nil;

}

@end
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
1

This one is shorter:

@interface MyLabel : UILabel
@end
@implementation MyLabel
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    if (self.backgroundColor == nil) {
        [super setBackgroundColor:backgroundColor];
    }
}
@end

Sets background color once while awaking from Nib

k06a
  • 17,755
  • 10
  • 70
  • 110
1

http://giorgiocalderolla.com/2011/04/16/customizing-uitableviewcells-a-better-way/

Dileep Reghu
  • 879
  • 7
  • 6
  • it seems like the article recommended putting all your subviews inside a container UIView and just have a single subview in the UITableViewCell. Not sure how that would solve my issue, but I'm gonna give it a try. – pixelfreak Jul 04 '11 at 22:28
  • I guess, what I really have to do is code my own selected behavior...hmm...wish there's a more straightforward way. – pixelfreak Jul 04 '11 at 22:38
1

Have you considered setting the cell selection style to none and overriding the cell's setHighlighted:(BOOL)highlighted method in a UITableViewCell subclass for better control? Reimplementing the blue background shouldn't be too difficult.

You can just set the cell view's background to blue (solid or a gradient) if highlighted = YES.

mjisrawi
  • 7,846
  • 3
  • 24
  • 27
  • Overriding ```setHighlighted:animated:``` , saving the old bg and resetting it after ```super``` did the trick. – dwery Jan 16 '14 at 17:59
0

You can set background color in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Worked for me.

Pavel
  • 642
  • 7
  • 13
  • this is called only when you lift the finger off the cell, while the background changes when you tap the cell – dwery Jan 16 '14 at 18:05
0

I know there are a bunch of answers on this old question. But to confirm the answer listed above works in iOS 10 here it is in swift.

override func layoutSubviews() {
 super.layoutSubviews()
 someLabel.backgroundColor = UIColor.orange
}
zsteed
  • 97
  • 2
  • 6
0

remove selection color using

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Dileep Reghu
  • 879
  • 7
  • 6
  • 1
    But I do want to keep the color. I am trying to figure out a way for the UILabel background color to show on top of the selection color. – pixelfreak Jul 04 '11 at 08:25
0

Try bringing your label to the front of the view.. you can do this by going to your custom cell .xib, clicking the label and going Editor, Arrangement, Bring to front.

To do that in code, its a little method.. Programmatically send to front/back elements created from interface builder

Community
  • 1
  • 1
Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
0

I have another hunch about this one, since you're setting the background color, and the cell is in the background of the view, and is a subview of that view.. there could be some weirdness going on there.

Sam Jarman
  • 7,277
  • 15
  • 55
  • 100