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

7

11

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];
Ineffaceable answered 10/8, 2011 at 16:38 Comment(0)
H
12

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"];
Houlberg answered 10/8, 2011 at 16:44 Comment(5)
thanks! However, it is not working as the color doesn't even appear in the cell nowIneffaceable
Interesting. That used to work. In that case, you’ll need to create an additional UIView with the appropriate frame (0, 0, cell.bounds.size.width, cell.bounds.size.height) and set its backgroundColor, then set that view as the cell’s backgroundView.Houlberg
But won't that cover up my disclosure indicator?Ineffaceable
No. backgroundView is placed in the background of the cell, below the content view and any accessory views like the disclosure indicator.Houlberg
the cell.background color =[UIColor colorWithHexString:@"#FFFFCC"] worked if I put the code in the method - (void)tableView: (UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath. It covers the area behind the disclosure as well.Ineffaceable
C
1

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

Cutpurse answered 10/8, 2011 at 16:45 Comment(2)
@Khelder thanks! but the color is not appearing at all in my cell.Ineffaceable
You can also set the color of the tableview itself, with tableview.backgroundColor, if it suits you better.Cutpurse
S
1

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.

Strobilaceous answered 30/5, 2014 at 13:30 Comment(0)
C
0

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.

Congeries answered 10/8, 2011 at 17:29 Comment(0)
C
0

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

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

Hope this helps someone

Chavaree answered 13/5, 2012 at 21:52 Comment(0)
I
0

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];
    }
}
Indiscriminate answered 24/9, 2013 at 5:17 Comment(0)
S
-1

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"];
Saskatchewan answered 10/8, 2011 at 16:50 Comment(2)
strangely, this has no effect and I am still seeing the white background behind my disclosure (cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator)Ineffaceable
The accessory view is for you to set. It doesn't get set when you only set the accessory type. So the expected behavior is for these assignments to have no effect (as you saw).Chantellechanter

© 2022 - 2024 — McMap. All rights reserved.