views:

224

answers:

4

I have a UIlabel on a UItabelViewCell, which I've created programmatically (ie 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 TableViewCell is highlighted?

A: 

Hi Jonathan,

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

Pugal Devan
A: 

the background of label is by default clearColor.. So try changing the color of the label.. tmpLabel.backgroundColor=[UIColor grayColor];

Happy Coding... ;)

Suriya
Actually it isn't, the default is white. The label's background gets set to clearColor when the cell it is becomes highlighted.
Jonathan
A: 

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.

Jonathan
A: 

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.

I never thought of that, it's a good idea. At the moment I have subclassed the UITableViewCell and overridden the highligh and selected methods to not change the background color of selected subviews, but it is quite a nasty 'hack'
Jonathan