Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
            }
        }
    }
like image 541
ytpm Avatar asked Feb 02 '12 19:02

ytpm


3 Answers

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

}
like image 122
Alex Stanciu Avatar answered Oct 19 '22 18:10

Alex Stanciu


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];
}
like image 29
Westley Avatar answered Oct 19 '22 19:10

Westley


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

like image 22
manuelBetancurt Avatar answered Oct 19 '22 18:10

manuelBetancurt