简体   繁体   中英

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;
            }
        }
    }

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];

}

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];
}

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!! ;)

Please try this, it works for me.

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

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

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

 cell.backgroundColor = [UIColor redColor];

在swift 3中你可以使用

cell.backgroundcolor = UIColor.white

尝试设置单元格backgroundColor属性,为我工作cell.backgroundColor=[UIColor blueColor];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM