Cannot change background color of UITableViewCell

I have UITableViewCellone that is suitable for UITableView, but somehow I can’t change the background color when I enter the edit style mode. I tried everything on the Internet, searched all day, but still couldn't fix it (I 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;
            }
        }
    }
+5
source share
9 answers

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

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

    cell.backgroundColor = [UIColor redColor];

}
+18
source

You need to change the tableView cellView element, not the cell view.

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

    cell.contentView.backgroundColor = [UIColor redColor];
}
+4
source

, ,

, UITableView,

, , , ,

, ,

, , ,

! [overkill , , !]

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 

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

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;

   }

!! ;)

+2

backgroundColor, cell.backgroundColor=[UIColor blueColor];

0

, , .

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

@Westley;

tableView cellView, .

:

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

, , ,

0

Make sure you have not set the background color of the content view in the storyboard. If you do, changing the cell.backgroundColor in the code will not make any difference, and you will get confused in the round (like me).

0
source
 cell.backgroundColor = [UIColor redColor];
0
source

in swift 3 you can use

cell.backgroundcolor = UIColor.white
0
source

All Articles