ChatGPT解决这个技术问题 Extra ChatGPT

Why doesn't UITableViewCell background color work (set in interface builder)?

Why doesn't UITableViewCell background color work (set in interface builder)?

I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?

You need to show how you're loading your UITableViewCell from nib, you may have a problem there.
I can confirm the OP -- strange. But notice, in IB, the UITableViewCell does not look like a plain version of a small View, it has an oval "content" area imposed on top of the background. I think this is an indication that there is something on top of the background view.
Yup, there's a load of stuff in there, including a view that is part of the table cell called "backgroundView".
One thing I found is that cell.backgroundColor = [UIColor redColor]; does work in didSelectRowAtIndexPath if your are using a grouped table style. plain doesn't work.

c
chris

It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:

cell.contentView.backgroundColor = [UIColor redColor];

Thanks man, I was just about to become insane, but you saved me! ;)
If you want to minimise the amount of styling you have to do in code, you can also set this using the User Defined Runtime Attributes section in Interface Builder - just use 'contentView.backgroundColor' for the Key Path, select 'Color' as the type, and then select a color using the Interface Builder color selector for the value. Just make sure if you're going to take this approach that you establish it as a standard on your team, or else you're going to end up creating a long mystery for some future developer who can't figure out where the heck the background color is getting set.
ios below ios7, must use cell.contentView.backgroundColor,but not cell.backgroundColor
question is why it doesn't work in IB. We know already that it works programmatically.
S
Shiprack

What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];

o
occulus

Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.

Recommended reading:

http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html

and then:

http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html


But when you're loading your own tableview cell from nib, you expect to get what you set up in Interface Builder. So, just which of the many views of the UITableViewCell are you editing when you load the cell from nib?
Not sure I understand your question. But when you load a UITableViewCell from a nib, you know which cell you're loading, because the method in question (cellForRowAtIndexPath:) gets passed in a NSIndexPath. You can customize a table cell, if necessary, once it has been loaded from the nib. And note that you can hook up the backgroundView etc. parts of UITableViewCell from within interface builder.
thanks occulus - I had guessed it was something to do with the number of views that in a UITableViewCell, but I was more looking for a simple answer why Apple would have you able to set what looks to be the overall "background" in Interface Builder, whereas in fact it doesn't seem to work. Is this really a usability bug in Interface Builder arguably for example?
I see what you mean Greg. It does seem slightly misleading that interface builder lets you set the background in the usual way, but remember also that a UITableViewCell as seen in IB has a dotted-oval outline with "Content view", so that is also a hint that this is a slightly unusual component in that it contains other views which make up its appearance.
thanks, it comforting to here the a experienced developer understands where I'm coming from :) So I'm not totally misunderstanding something. Perhaps the simple answer is that Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers), and hence they felt the need to properly customise the interface for each scenario (versus leave generic) was not worth it given this...make sense?
S
Steph Sharp

You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.

This Works !!


P
Prince Kumar Sharma
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];

some explanation would help this answer
m
mbinette

Make sure before defining a cell color your backgroundView is null

cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];

B
Bogatyr

This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.

Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.


thanks for the info Bogatyr - I won't accept just for the fact the question is searching for a layman's term type answer re why IB is setup the way it is - e.g. Is this really a usability bug in Interface Builder arguably for example?
C
Csabi

It works perfectly, if you pick the tableview and set background to for example red the whole table will be red. maybe you do not linked the table or something

hope it helped


Not how you do it. Even if your approach appears to work, it's a bad idea. See my answer.
hi, i picked the table view in IB in View section I set background to a color and simply saved it.and it works
To rephrase -- his question wasn't "How do I change background of whole table". Hsi question is about the background of a table cell.
His question was : "But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?"in IB you can set background color but you are right it works for whole table not for single cell like it is written in my answer: "the whole table will be red"
thanks Csabi - this is useful info - I might leave the question open still however re the specific question I was asking - whether there is an answer a non-Apple person knows I'm not sure
G
Greg

(try this)

Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.


S
Splendid

Y not, use setBackgroundColor

self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];

S
Symmetric

You can partially do this in Interface Builder (probably added to Xcode recently):

Expand the cell in the Storyboard's Document Outline and select "Content View"

Set Content View background color in the Attributes Inspector

Select each content item in the cell and set their background colors to ClearColor

Still don't see a way to set the accessory background though.


O
Odd-Arne

I did get this issue also. In IB you get the ability to change the background color of the cell when choosing cell. This is NOT correct. You should go to the document outline on the left of the storyboard and choose the content view inside the cell. Change the background of the content view. You should now get correct cell color.


H
Hemanshu Liya
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}

We need to change the color in this method.


佚名

Try this it works for me:

- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
    tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"Cream.jpg"]];
}

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now