Sheehan Alam
Sheehan Alam

Reputation: 60859

Setting background color of grouped UITableViewCell

I have a grouped UITableView. I am trying to make a custom UITableViewCell background:

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

        self.contentView.opaque = YES;
        self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rowbg@2x"]];

        //Change Indicator
        CGRect rect;
        rect = CGRectMake(0, 0, 10, 50);
        changeImageIndicator = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview: changeImageIndicator];

        //Symbol
        rect = CGRectMake(10, 10, 200, 20);
        symbolLabel = [[UILabel alloc] initWithFrame:rect];
        symbolLabel.textAlignment = UITextAlignmentLeft;
        symbolLabel.font = [UIFont fontWithName:@"Arial" size:22];
        symbolLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        symbolLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview: symbolLabel];

        //Company
        rect = CGRectMake(10, 30, 180, 20);
        companyLabel = [[UILabel alloc] initWithFrame:rect];
        companyLabel.textAlignment = UITextAlignmentLeft;
        companyLabel.font = [UIFont fontWithName:@"Arial" size:13];
        companyLabel.adjustsFontSizeToFitWidth = YES;
        companyLabel.minimumFontSize = 10.0; 
        companyLabel.backgroundColor = [UIColor clearColor];
        companyLabel.textColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0];
        [self.contentView addSubview: companyLabel];

        //Price
        rect = CGRectMake(190, 10, 100, 20);
        priceLabel = [[UILabel alloc] initWithFrame:rect];
        priceLabel.textAlignment = UITextAlignmentRight;
        priceLabel.font = [UIFont fontWithName:@"Arial" size:20];
        priceLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        priceLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview: priceLabel];

        //Change
        rect = CGRectMake(190, 30, 100, 20);
        changeLabel = [[UILabel alloc] initWithFrame:rect];
        changeLabel.textAlignment = UITextAlignmentRight;
        changeLabel.font = [UIFont fontWithName:@"Arial" size:15];
        changeLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
        changeLabel.adjustsFontSizeToFitWidth = YES;
        changeLabel.backgroundColor = [UIColor clearColor];
        changeLabel.minimumFontSize = 10.0; //adjust to preference obviously

        [self.contentView addSubview: changeLabel];
    }
    return self;
}

The background color bleeds past the rounded corners. See image: enter image description here

How can I make this not bleed?

Upvotes: 2

Views: 3059

Answers (7)

Dmitry Sokurenko
Dmitry Sokurenko

Reputation: 6132

Set the cell background color in the tableView:cellForRowAtIndexPath:

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage  imageNamed:@"Gradient.png"]]; 

Upvotes: 0

human4
human4

Reputation: 154

This works for iOS 3.0 and later:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor redColor];
}

Upvotes: 4

Sheehan Alam
Sheehan Alam

Reputation: 60859

I had to create a rounded top, middle and button graphic image and set it to the background view of the cell depending on which row it is.

Upvotes: 0

picciano
picciano

Reputation: 22701

Have you tried setting the backgroundColor of the backgroundView rather than the contentView?

Upvotes: 0

lbrndnr
lbrndnr

Reputation: 3389

self.contentView.superview.opaque = YES; self.contentView.superview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"rowbg@2x"]];

Upvotes: 0

lbrndnr
lbrndnr

Reputation: 3389

What about self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; ?

Upvotes: 1

Dashzaki
Dashzaki

Reputation: 568

This worked for me when nothing else did. Set the background color in IB to the grouped Table view color (default). Then in code, set the color to clearColor. I also have the cells marked opaque=NO and clearsContextBeforeDrawing=NO, but those settings alone didn't change anything until I added the clearColor by code – Bdebeez

Upvotes: 0

Related Questions