Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
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?
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...
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...
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 :(
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.
// Set up the cell UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; bg.backgroundColor = [UIColor groupTableViewBackgroundColor]; // or any color cell.backgroundView = bg; [bg release];
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!
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.
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.
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]]; }
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.
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 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.
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!
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.
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.
Replies
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...
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDoug
ThorJupiter Software
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIt 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 :(
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome1. 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!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeiO
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeit 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!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome[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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomecustom-uitableviewcell-color-issue
Hope it helps
-John
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYou must not set the color in the cellForRowAtIndexPath.
This works for both the plain and grouped style :
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."
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeAny suggestions ?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome