Change background of a UITableViewCell

I was asked this question by a co-worker on how to do this. My first answer was when you init the cell just set the backgroundColor but that did not work at all.

If you followed my other tutorials and have a custom UITableViewCell open that file and in the initWithFrame method you want the following

MyContentView.backgroundColor = [UIColor redColor];

About mike
Currently works for OpenSky as a Senior Linux Admin. He has a wonderful wife Thanuja and 2 great dogs. His major side project is Photoblog.

Comments

5 Responses to “Change background of a UITableViewCell”
  1. Pete says:

    Is anything besides setting the background to another color? Like setting the background to a certain image? Just like themes…

  2. Alex Hunt says:

    Did you manage to do this as I am trying to do the same.

    Existing code:
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    I am trying to replace the background colour with a picture.
    Thanks for any help

  3. giku says:

    Just create new UIView, set it as table cell backgroundView and use backgroundColor of this UIView.

  4. Ezra Epstein says:

    Don’t set the background color when the cell is created as the color is controlled at that point by the table. Rather implement the delegate method and set the background color there. Very easy this way.

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if (indexPath.row == 1) {
    [cell setBackgroundColor:[UIColor yellowColor]];
    }
    }

  5. Ivalene says:

    I see, I spuopse that would have to be the case.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!