Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

AppFusion - 6 in 1!
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2008, 01:38 PM   #1 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 33
Default UITableViewCell backgroundColor

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?
tmbg is offline   Reply With Quote
Old 10-10-2008, 03:16 PM   #2 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

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...
rames44 is offline   Reply With Quote
Old 10-10-2008, 03:37 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Location: Austin, TX
Posts: 30
Default

Quote:
Originally Posted by tmbg View Post
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
dougdawson is offline   Reply With Quote
Old 10-10-2008, 03:57 PM   #4 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 33
Default

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
tmbg is offline   Reply With Quote
Old 10-10-2008, 04:42 PM   #5 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

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.
PhoneyDeveloper is offline   Reply With Quote
Old 10-14-2008, 11:19 AM   #6 (permalink)
Registered Member
 
Join Date: Oct 2008
Location: Berlin, Germany
Posts: 7
Default

Yes, I do it like that:
Code:
// Set up the cell
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor groupTableViewBackgroundColor]; // or any color
cell.backgroundView = bg;
[bg release];
ziad is offline   Reply With Quote
Old 10-14-2008, 11:53 AM   #7 (permalink)
New Member
 
Join Date: Oct 2008
Posts: 33
Default

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...
tmbg is offline   Reply With Quote
Old 10-14-2008, 12:07 PM   #8 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 356
Default

how can you have background color and round corners?
erotsppa is offline   Reply With Quote
Old 11-20-2008, 01:03 AM   #9 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 148
Default

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!
john855 is offline   Reply With Quote
Old 11-20-2008, 07:17 AM   #10 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 394
Default

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.
__________________
Super Pig
iOwn - Inventory anything and everything.
detz is offline   Reply With Quote
Old 11-20-2008, 12:38 PM   #11 (permalink)
Registered Member
 
Forsworn's Avatar
 
Join Date: Oct 2008
Location: Germany
Posts: 504
Default

Quote:
Originally Posted by erotsppa View Post
how can you have background color and round corners?
I'm stuck at that, too...
Forsworn is offline   Reply With Quote
Old 03-19-2009, 03:39 PM   #12 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 21
Default

Quote:
Originally Posted by Forsworn View Post
I'm stuck at that, too...
did anyone get anywhere on this? i can change the color, but not without loosing my rounded corner.
stguitar is offline   Reply With Quote
Old 03-31-2009, 12:49 PM   #13 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 21
Default

Quote:
Originally Posted by stguitar View Post
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!
stguitar is offline   Reply With Quote
Old 09-10-2009, 04:43 AM   #14 (permalink)
Registered Member
 
gblank's Avatar
 
Join Date: Oct 2008
Location: Netherlands
Posts: 35
Send a message via Skype™ to gblank
Default UITableViewCell background color or image

Quote:
Originally Posted by stguitar View Post
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.
gblank is offline   Reply With Quote
Old 10-11-2009, 05:41 AM   #15 (permalink)
Registered Member
 
Join Date: Feb 2009
Location: India
Posts: 137
Default

here is a link on how to customize your tabelview cells
custom-uitableviewcell-color-issue
Hope it helps


-John
JohnMabassa is offline   Reply With Quote
Old 10-11-2009, 02:57 PM   #16 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 30
Default

Quote:
Originally Posted by tmbg View Post
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 :

Code:
- (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."

Last edited by colionel; 10-11-2009 at 02:59 PM.
colionel is offline   Reply With Quote
Old 09-14-2010, 10:12 PM   #17 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 1
Default

Quote:
Originally Posted by colionel View Post
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 :

Code:
- (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!
eyesonly is offline   Reply With Quote
Old 09-30-2010, 03:15 AM   #18 (permalink)
Registered Member
 
Jules2010's Avatar
 
Join Date: Apr 2010
Location: UK
Posts: 157
Default

Quote:
Originally Posted by colionel View Post
Code:
- (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 ?



Any suggestions ?
Jules2010 is offline   Reply With Quote
Old 12-07-2010, 04:03 AM   #19 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 4
Default

Quote:
Originally Posted by eyesonly View Post
I've just registered to this forum just to say this: You rock man! It's the most elegant solution, thx!
great!
boboboa32 is offline   Reply With Quote
Old 01-08-2011, 10:13 AM   #20 (permalink)
What?
 
Join Date: Nov 2010
Posts: 5
Talking

Quote:
Originally Posted by colionel View Post
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 :

Code:
- (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.
Poatface is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 662
24 members and 638 guests
ADY, bandley, BrianSlick, Creativ, dacapo, Dattee, djbrooks111, gbenna, HDshot, IphoneSdk, iseff, jakerocheleau, jbro, joeallenpro, kampftrinker, linkmx, mer10, nimesh_158, Reyna, ukneeq, yurikus
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 160,619
Threads: 89,833
Posts: 383,490
Top Poster: BrianSlick (7,244)
Welcome to our newest member, yurikus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:42 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0