ytpm
ytpm

Reputation: 5160

can't change UITableViewCell background color

I have UITableViewCell that fits on UITableView, but somehow I can't change the background color when entering editing style mode.. I've tried everything on the web, search all day long, but still couldn't get it fixed (I've searched StackOverflow as-well). please, help me.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"MainTableViewCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (MainTableViewCell *)view;
            }
        }
    }

Upvotes: 5

Views: 9794

Answers (9)

user8043247
user8043247

Reputation:

in swift 3 you can use

cell.backgroundcolor = UIColor.white

Upvotes: 0

Deep
Deep

Reputation: 436

 cell.backgroundColor = [UIColor redColor];

Upvotes: 0

dev4life
dev4life

Reputation: 1170

Be sure you didn't already set the content view's background color in the Storyboard. If you did, changing the cell.backgroundColor in code will make no difference, and you will go round and round in confusion (like I did).

Upvotes: 0

Westley
Westley

Reputation: 1151

You need to change the tableView cell's contentView, not the cell's background view.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.contentView.backgroundColor = [UIColor redColor];
}

Upvotes: 5

Paulo Almeida
Paulo Almeida

Reputation: 2154

As @Westley mentioned;

You need to change the tableView cell's contentView, not the cell's background view.

This is how you should do it:

    if(index == conditionYouWant)
    {
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    else
    {
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.925 green:0.933 blue:0.937 alpha:1.0];
    }

Hope it helps you guys out

Upvotes: 0

hieund
hieund

Reputation: 218

Please try this, it works for me.

UIView *bgView = [[UIView alloc] initWithFrame:cell.frame];
bgView.backgroundColor = [UIColor greenColor];
cell.backgroundView = bgView;
[bgView release];

Upvotes: 0

manuelBetancurt
manuelBetancurt

Reputation: 16168

to have total control on the customisation of your cells, I prefer better to override the cell view,

create a new class for your cell view, that gets called by the UITableView,

later when i get to my work computer if you havent found your answer I will post some sample code,

once you see how it works is pretty easy

you could as well place an image for your cell background, and place different labels and images, buttons, textfields in custom places of your cell,

EDIT>>

the code! [is overkill just to change the background, but if you want to really customise your cell, this is the way to go!]

in your CustomCell.h

#import <UIKit/UIKit.h>
@interface CustomCell : UITableViewCell {
UILabel *_kLabel;
UILabel *_dLabel;
}
@property (nonatomic, retain) UILabel *kLabel;
@property (nonatomic, retain) UILabel *dLabel;
- (void) initLabels;
@end 

in your CustomCell.m

#import "CustomCell.h"
@implementation CustomCell
@synthesize kLabel = _kLabel;
@synthesize dLabel = _dLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code

    CGRect popUpImageBgndRect = CGRectMake(0, 0, 942, 44);
    UIImageView *popUpImageBgnd = [[UIImageView alloc] initWithFrame:popUpImageBgndRect];
    [popUpImageBgnd setImage:[UIImage imageNamed:@"tableCellBgnd.png"]];
    popUpImageBgnd.opaque = YES; // explicitly opaque for performance
    [self.contentView addSubview:popUpImageBgnd];
    [popUpImageBgnd release];

    [self initLabels];
     }
     return self;
     }

    - (void)layoutSubviews {

[super layoutSubviews];

CGRect contentRect = self.contentView.bounds;

CGFloat boundsX = contentRect.origin.x;

CGRect frame;


frame= CGRectMake(boundsX+10 ,10, 200, 20);

self.kLabel.frame = frame;


frame= CGRectMake(boundsX+98 ,10, 100, 20);  

self.dLabel.frame = frame;

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
 {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
 }

  - (void) initLabels {
self.kLabel = [[[UILabel alloc]init] autorelease];
self.kLabel.textAlignment = UITextAlignmentLeft;
self.kLabel.backgroundColor = [UIColor clearColor];
self.kLabel.font = [UIFont fontWithName:@"FS Albert" size:16];
self.kLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];

self.dLabel = [[[UILabel alloc]init] autorelease];
self.dLabel.textAlignment = UITextAlignmentLeft;
self.dLabel.backgroundColor = [UIColor clearColor];
self.dLabel.font = [UIFont systemFontOfSize:16];
self.dLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];

[self.contentView addSubview:self.kLabel];
[self.contentView addSubview:self.dLabel];

 }

-(void) dealloc {

 [_kLabel release];
 [_dLabel release];

[super dealloc];
 }

  @end

And in your ViewController.m

YourViewController.m

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";



CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

  }


   return cell;

   }

ENJOY!! ;)

Upvotes: 2

Alex
Alex

Reputation: 5210

Try customizing the cell inside tableView: willDisplayCell: method, something like this:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.backgroundColor = [UIColor redColor];

}

Upvotes: 19

Daniel
Daniel

Reputation: 22405

try setting the cells backgroundColor property, worked for me cell.backgroundColor=[UIColor blueColor];

Upvotes: 0

Related Questions