Zhen
Zhen

Reputation: 12431

Objective C: How to add color to entire cell (with a disclosure indicator)

I am trying to add background color to my cell but was not able to color the portion with the disclosure indicator (see screenshot below)

enter image description here

What can I do to color the entire cell? My implementation as follows.

cell.contentView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

//Clear colors for labels
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];

Upvotes: 11

Views: 6280

Answers (7)

Cameron Lowell Palmer
Cameron Lowell Palmer

Reputation: 22245

UIImageView

UIImageView with a chevron image on the right-hand side. Preferably in the storyboard, but it can easily be managed in code by attaching it to the content view.

Upvotes: 1

Ron
Ron

Reputation: 271

This drove me nuts. I tried a number of different approaches in the cellForRowAtIndexPath method. I finally found the way to do it is to override the willDisplayCell method like so:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row % 2) {
        cell.backgroundColor = [UIColor whiteColor];
    } else {
        cell.backgroundColor = [UIColor colorWithRed:225.0/255.0 green:225.0/255.0 blue:225.0/255.0 alpha:1.0];
    }
}

Upvotes: 0

Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

The accepted answer didn't work for me, but the following code worked :

[cell.contentView setBackgroundColor:[UIColor whiteColor]];

Hope this helps someone

Upvotes: 0

Dave.B
Dave.B

Reputation: 6652

color a uiview with the same bounds as the cell and add it as a subview




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    UIView *colorView = [cell viewWithTag:200];
    if(!colorView){
        colorView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)] autorelease];
        colorView.backgroundColor = [UIColor redColor];
        colorView.tag = 200;
        [cell addSubview:colorView];
    }

    return cell;
}


then add all of your subviews to that colored view.

Upvotes: 0

highlycaffeinated
highlycaffeinated

Reputation: 19867

Being that its a UIView subclass, you should be able to set the background color of the accessoryView:

cell.accessoryView.backgroundColor = [UIColor clearColor];

or:

cell.accessoryView.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

Upvotes: -1

Kheldar
Kheldar

Reputation: 5389

My code uses (in the cell itself) self.backgroundColor, so you should uses cell.backgroundColor instead of cell.contentView.backgroundColor. The contentView does not include the selection indicator view, which you have guessed I suppose ;)

If you only want it to change colors if selected, use selectedBackgroundView.backgroundColor

Upvotes: 1

Noah Witherspoon
Noah Witherspoon

Reputation: 57139

UITableViewCell subclasses UIView; you can set its background color just as you can any other view’s. Cells in a “grouped”-style table view require some more work there, but it doesn’t look like you’re using that, so:

cell.backgroundColor = [UIColor colorWithHexString:@"#FFFFCC"];

Upvotes: 12

Related Questions