Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background color of UITableViewCell

I have looked around to find a solution for setting the background color of the accessoryView to the same background color as the cell´s contentView.

    cell.contentView.backgroundColor = [UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];
    cell.accessoryView.backgroundColor =[UIColor colorWithRed:178/255.f green:14/255.f blue:12/255.f alpha:0.05];

There is a solution that works but only let me use one color for all cells.

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

Is the only solution to not use the accessoryView and use an image instead?

Thanks!

like image 407
Silversnail Avatar asked Jul 11 '11 11:07

Silversnail


People also ask

How do I change the background color in a table?

For changing the background color of the table view cell, you should change the contentView. backgroundColor property of the cell. Now run the project to see the effect.

How do I change the background color of a selected cell in Swift?

Swift 3, 4, 5 select cell background colour Show activity on this post. Next connect your cell's selectedBackgroundView Outlet to this view. You can even connect multiple cells' outlets to this one view. Save this answer.

How do I change the background color in Xcode?

At the top select the attributes inspector. Under the section "View" there should be a spot that says "Background". click that and choose your colour.

How do you change the background color of a cell?

Select the cell or range of cells you want to format. Click Home > Format Cells dialog launcher, or press Ctrl+Shift+F. On the Fill tab, under Background Color, pick the color you want. To use a pattern with two colors, pick a color in the Pattern Color box, and then pick a pattern in the Pattern Style box.


1 Answers

I struggled with this one for a little while too and resorted to creating a custom image with the accessory. But I just found this solution that works well and doesn't require a custom image. The trick is to change the cell's backgroundView color not the backgroundColor.

UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2) {
    myView.backgroundColor = [UIColor whiteColor];
} else {
    myView.backgroundColor = [UIColor blackColor];
}
cell.backgroundView = myView;

No need to change the accessoryView or contentView background colors. They'll follow automatically.


Note for 2014. Very typically you wold use -(void)setSelected:(BOOL)selected animated:(BOOL)animated

So, you'd have a custom cell class, and you'd set the colours for the normal/selected like this...

HappyCell.h
@interface HappyCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *mainLabel;
etc...
@end

HappyCell.m
@implementation HappyCell

-(id)initWithStyle:(UITableViewCellStyle)style
      reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
        {
        }
    return self;
    }

-(void)awakeFromNib
    {
    }

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

    if(selected)
        {
        self.backgroundColor = [UIColor redColor];
        .. other setup for selected cell
        }
    else
        {
        self.backgroundColor = [UIColor yellowColor];
        .. other setup for normal unselected cell
        }
    }

@end

// to help beginners.......
// in your table view class, you'd be doing this...

-(NSInteger)tableView:(UITableView *)tableView
      numberOfRowsInSection:(NSInteger)section
    {
    return yourDataArray.count;
    }

-(UITableViewCell *)tableView:(UITableView *)tv
      cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSInteger thisRow = indexPath.row;
    ContentsCell *cell = [tv
      dequeueReusableCellWithIdentifier:@"cellName"
      forIndexPath:indexPath]; 
    // "cellName" must be typed in on the cell, the storyboard
    // it's the "identifier", NOT NOT NOT the restorationID

    [cell setupForNumber: thisRow];
    cell.mainLabel.text = yourDataArray[ thisRow ][@"whatever"];
    cell.otherLabel.text = yourDataArray[ thisRow ][@"whatever"];

    return cell;
    }

hope it helps someone.

like image 82
Sheila Santos Avatar answered Sep 19 '22 17:09

Sheila Santos