23/07/09

How to change the UITableViewCell backgroundColor / backgroundView

Filed under: iPhone Dev SDK by Marc @ 08:36

I recently found out that the properties backgroundColor and backgroundView of an UITableViewCell can’t be changed easily, or at least using the setBackgroundColor message and setBackgroundView.

If you try the setBackgroundColor nothing happens and if you try the setBackgroundView a white rectangle appears just under the textLabel I think it’s the textLabel backgroundColor, but changing this property to [UIColor ClearColor](that means transparent), doesn’t do anything.

Surfing the forums I found one solution that works for me and I’d like to share. In my case I was re-writting the UItableViewCell definition to build my own “Custom UITableViewCell” ’cause I need to display the information I had in an specific way, if this is your case too, you can override the layoutSubviews method to this one:

- (void)layoutSubviews {     
    [super layoutSubviews];     
    [self setBackgroundColor:[UIColor clearColor]];
}

This changes the background to transparent and allows to show up my backgroundView. If you don’t want to change the backgroundView and you just want to change the backgroundColor you just have to set up the color you like where I set up the “clearColor”



1 Comment

  1. And if you don’t want to create a custom subclass of UITableViewCell, N.Berendt has a good solution using one of the UITableView delegate methods:
    http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell/1220985#1220985

    Comment by Chris by 02/02/10 @ 22:36

RSS feed for comments on this post. TrackBack URL

Sorry, the comment form is closed at this time.