Can't Set Background Color of Uitableviewcell in Ib

Can't set background color of UITableViewCell in IB

There's an easier solution: When you create a UITableViewCell in Interface Builder, simply drag and drop an additional UIView so that it covers the entire UITableViewCell you're creating within IB. Place your additional UI elements on top of this additional UIView. You can set this extra UIView to whatever background you like.

There are quite a few answers on StackOverflow that suggest you change the background color of your UITableViewCell by using a line of code like this one:

cell.contentView.backgroundColor = [UIColor redColor];

If you add a drop shadow to your cell by adjusting the contentView's frame size slightly, you may find that this line will change both the background color of your cell and the color of your drop shadow area as well.

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

(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.

UITableViewCell backgroundColor set in IB, shows up black when it's set to white

Yeah this happens quite frequently. It's not only related to the UITableViewCell but it can happen on all UIViews.

I've stopped using the default value and instead select the actual color even though they are the same. It is only when selecting default it will give this kind of results.

Change UITableViewCell background color in Interface builder

You cannot do that unfortunately.

Change the background colour of the cell in tableView:willDisplayCell:forRowAtIndexPath:

From the documentation of UITableViewCell:

Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.

UITableViewCell backgroundColor set in IB, shows up black when it's set to white

Yeah this happens quite frequently. It's not only related to the UITableViewCell but it can happen on all UIViews.

I've stopped using the default value and instead select the actual color even though they are the same. It is only when selecting default it will give this kind of results.

UITableViewCell show white background and cannot be modified on iOS7

As Apple DOC said (UITableViewCell Class Reference):

... In iOS 7, cells have a white background by default; in earlier versions of iOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, do so in the tableView:willDisplayCell:forRowAtIndexPath: method of your table view delegate.

So for my case that to show cells with transparent background, just need to implement the delegate method in the table view controller like below:

- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}

Just Note: As @null said, "...there seems to be a bug in interface builder...", I'm not totally sure whether it does have the bug, but seems so cause his comment got several up votes. So there might something wrong if you use IB. :)

set background color of footerView UI TableView doesn't work

I think you must set a frame for footer view. If you just only create UIView then the default of Apple will create UIView with frame is zero. You need to set width, height, x, y for it to display in TableView

 let frame = CGRect(x: 0, y: 0, width: yourWidthExpect, height: yourHeightExpect))
let footerView = UIView(frame: frame)

After that you can set background color and do anything.

How to set custom background color of UITableViewCell even there's no data inside

I used the following code to display cell alternative color even if cell is not initialized.I have done this work on scrollViewDidScroll as showing below:--

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIView *view=[[UIView alloc] initWithFrame:tblView.frame];
view.backgroundColor=[UIColor greenColor];

UIView *cellView;

int y=0;
int i=0;

for (UIView *view in tblView.subviews) {
if ([NSStringFromClass([view class]) isEqualToString:@"_UITableViewSeparatorView"]) {

cellView=[[UIView alloc] initWithFrame:CGRectMake(0, y, 320, 44)];

if (i%2==0) {
cellView.backgroundColor=[UIColor redColor];
}else{
cellView.backgroundColor=[UIColor greenColor];
}

[view addSubview:cellView];
i++;
}
}

tblView.backgroundView=view;
}

And got the correct result on scrolling table view. But the problem is it works when user scrolls the tableView atleast once a time.
If you will get success to fire event on tableView completes its reloading.Then it will be fine.

Here is output I got on scrolling tableView.

Sample Image

I also write this method to call didScrollMethod manually but doesn't seems to work perfectly.

[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];

But calling method like code below absolutely works fine.

- (void)viewDidLoad
{
tblView=[[MyFirstView alloc] init];
tblView.delegate=self;
[tblView setFrame:self.view.frame];

[self.view addSubview:tblView];

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)viewDidAppear:(BOOL)animated{
[tblView.delegate scrollViewDidScroll:(UIScrollView*)tblView.superclass];
}

Means after loading tableView in viewDidLoad call didScroll in viewDidAppear works fine.

Insert below code if fluctuates first row while scrolling.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc] init];
return view;
}


Related Topics



Leave a reply



Submit