Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITableViewCell backgroundColor

tmbgtmbg Posts: 33Registered Users
edited January 2011 in iPhone SDK Development
I want to be able to set the background color of individual table cells programmaticaly... I've tried a bunch of different things, and nothing works quite how I want. I've tried [cell setBackgroundColor:color], I've tried [[cell contentView] setBackgroundColor:color], I've tried iterating over the cell's subviews and setting their color, I've tried iterating over the contentView's subviews and setting their color...

Best I've been able to do is make the color change on the little sliver of padding where the cell's image would be.

How would I go about changing its background color without subclassing UITableViewCell?
Post edited by tmbg on

Replies

  • rames44rames44 Posts: 365Tutorial Authors, Registered Users @ @
    edited October 2008
    I ran into the same issue in an application I'm working on, and never found a good solution. I could get parts of the cell to paint the background color, but not all of it. I then took a different approach, so I didn't pursue it.

    One possibility I didn't try, but might work for you - you could conceivably create a custom view that covers the entire table cell, install it into the cell, and have it opaque with your desired color, then, handle the positioning of images/text/whatever yourself as subviews of your custom view. That would be a little simpler than subclassing UITableViewCell, I suspect.

    At worst, you might have to subclass UITableViewCell in order to be able to override layoutSubviews so that you can make sure your view exactly matches the size of the UITableViewCell...
    For a little fun, check out my <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=298866985&mt=8" target="_blank">Biorhythms</a> app
  • dougdawsondougdawson Posts: 30Registered Users
    edited October 2008
    tmbg wrote: »
    I want to be able to set the background color of individual table cells programmaticaly... I've tried a bunch of different things, and nothing works quite how I want. I've tried [cell setBackgroundColor:color], I've tried [[cell contentView] setBackgroundColor:color], I've tried iterating over the cell's subviews and setting their color, I've tried iterating over the contentView's subviews and setting their color...

    Best I've been able to do is make the color change on the little sliver of padding where the cell's image would be.

    How would I go about changing its background color without subclassing UITableViewCell?

    Go ahead and subclass UITableViewCell. It's not a big deal, and gives you all kinds of new possibilities. Mmmmm...

    Doug
    <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=313853083&mt=8" target="_blank">RunningTotal</a>, finance app of the gee-gawds<br />
    <a href="http://www.thorjupitersoftware.com" target="_blank">ThorJupiter Software</a>
  • tmbgtmbg Posts: 33Registered Users
    edited October 2008
    This is going to get very frustrating VERY quickly, I fear...

    It looks like all the stock images that go along with a cell, such as the disclosure indicator, insert/delete controls, and the reordering control, are in their own views. Further, I think they might be images with a white background. The docs suggest not using transparency in a cell.

    I'm using all these controls to support navigation and editing... I need to be able to arbitrarily color the background of the cell, but I'm not sure how I'm going to be able to make this work :(
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited October 2008
    You can add a background view to the tableview cells. If you build your own UIView subclass that just fills itself with its background color that might work.
  • ziadziad Posts: 7New Users
    edited October 2008
    Yes, I do it like that:
    // Set up the cell
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor groupTableViewBackgroundColor]; // or any color
    cell.backgroundView = bg;
    [bg release];
    
  • tmbgtmbg Posts: 33Registered Users
    edited October 2008
    I'll try that, but the issue I was having is that when editing, the editing controls push in and have white backgrounds, and it looks terrible...
  • erotsppaerotsppa Posts: 357Registered Users Noob
    edited October 2008
    how can you have background color and round corners?
  • john855john855 Posts: 148Registered Users
    edited November 2008
    I've tried just about everything to get a background color to show up in a UITableViewCell, including:

    1. Setting the background color of the cell to [UIColor yellowColor]
    2. setting the backgroundview to a new uiview with a backgroundcolor
    3. Making the contentview and its UILabel subview non-opaque
    3b. setting the background color of those to a UIColor
    4. making every subview of the contentview clearcolor
    5. permutations of the above

    I can get the background color to work with no text, but as soon as I add text to the internal UILabel of the uitableviewcell (via the cell.text property) the only thing that stays the background color is the 10pixel sliver on the left (which is just how far over the tableviewcell is indented).

    Thus, it appears the uilabel is covering up the background color. I've researched this for a couple hours already, ready to ask for help!
  • detzdetz Posts: 394Registered Users
    edited November 2008
    Like you're added a new view you should add a new UILabel to put your text in too, don't use the cell.text property. Set the new UILabel background to transparent and set your color to what ever you want.
  • ForswornForsworn Posts: 504New Users
    edited November 2008
    erotsppa wrote: »
    how can you have background color and round corners?

    I'm stuck at that, too...
  • stguitarstguitar Posts: 21Registered Users
    edited March 2009
    Forsworn wrote: »
    I'm stuck at that, too...

    did anyone get anywhere on this? i can change the color, but not without loosing my rounded corner.
  • stguitarstguitar Posts: 21Registered Users
    edited March 2009
    stguitar wrote: »
    did anyone get anywhere on this? i can change the color, but not without loosing my rounded corner.

    i found a fix for this, and according to the articles, its the only way to do it (approved by apple engineers).

    it uses a custom view class created by Mike Akers here, and the code to show you how to go about implementing by Joao Prado can be found here.

    once you look at it for a minute, its easy to see how to use and tweak it for what you need. it is really something that should be provided in the UI framewokrs.

    to change the colors used for the cell, just set new colors for the custom view class's fillColor and backgroundColor properties.

    Kudos to Mr Akers and Prado!
  • gblankgblank Posts: 35Registered Users
    edited September 2009
    stguitar wrote: »
    i found a fix for this, and according to the articles, its the only way to do it (approved by apple engineers).

    it uses a custom view class created by Mike Akers here, and the code to show you how to go about implementing by Joao Prado can be found here.

    once you look at it for a minute, its easy to see how to use and tweak it for what you need. it is really something that should be provided in the UI framewokrs.

    to change the colors used for the cell, just set new colors for the custom view class's fillColor and backgroundColor properties.

    Kudos to Mr Akers and Prado!
    Here is the simpler fix I think:
    [myCell.contentView setbackgroundColor:[ UIColor greenColor ]];

    you do have to subclass your TableViewCell class and in your own cell class do this :
    - (void)layoutSubviews {
    [super layoutSubviews];
    [self setBackgroundColor:[UIColor clearColor]];
    }

    that's it.
  • JohnMabassaJohnMabassa Posts: 138Registered Users
    edited October 2009
    here is a link on how to customize your tabelview cells
    custom-uitableviewcell-color-issue
    Hope it helps


    -John
  • colionelcolionel Posts: 30Registered Users
    edited October 2009
    tmbg wrote: »
    How would I go about changing its background color without subclassing UITableViewCell?

    This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method.
    You must not set the color in the cellForRowAtIndexPath.

    This works for both the plain and grouped style :
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    	cell.backgroundColor = [UIColor redColor];
    }
    


    P.S: Here the documentation extract for willDisplayCell :

    "A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."
  • eyesonlyeyesonly Posts: 1New Users
    edited September 2010
    colionel wrote: »
    This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method.
    You must not set the color in the cellForRowAtIndexPath.

    This works for both the plain and grouped style :
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    	cell.backgroundColor = [UIColor redColor];
    }
    


    P.S: Here the documentation extract for willDisplayCell :

    "A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."

    I've just registered to this forum just to say this: You rock man! It's the most elegant solution, thx!
  • Jules2010Jules2010 Posts: 161Registered Users @ @
    edited September 2010
    colionel wrote: »
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    	cell.backgroundColor = [UIColor redColor];
    }
    

    The only thing is I get a white line in between cells ?

    20100930-xie2f5kusq8nhbqgwhhxmbrhgs.jpg

    Any suggestions ?
  • boboboa32boboboa32 Posts: 4New Users
    edited December 2010
    eyesonly wrote: »
    I've just registered to this forum just to say this: You rock man! It's the most elegant solution, thx!

    great!:D
  • PoatfacePoatface Posts: 5New Users
    edited January 2011
    colionel wrote: »
    This is really simple, since OS 3.0 just set the background color of the cell in the willDisplayCell method.
    You must not set the color in the cellForRowAtIndexPath.

    This works for both the plain and grouped style :
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    	cell.backgroundColor = [UIColor redColor];
    }
    


    P.S: Here the documentation extract for willDisplayCell :

    "A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."

    This is fantastic! Thank you so much for posting this.
Sign In or Register to comment.