简体   繁体   中英

Setting background color of a table view cell on iPhone

I want to achieve the effect where one cell of the table view will have blue background, the next one will have white, the next one will have blue again, and then white and so on... could you let me know how can I do that?

Thanks.

Add this method to your table view delegate:

#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView 
  willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath
{
    cell.backgroundColor = indexPath.row % 2 
        ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
        : [UIColor whiteColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}

You have to set the background color of the cell's content view

cell.contentView.backgroundColor = [UIColor colorWithRed...]

This will set the background of the whole cell.

To do this for alternate cells, use the indexPath.row and % by 2.

If you want to set cell color based on some state in the actual cell data object, then this is another approach:

If you add this method to your table view delegate:

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

Then in your cellForRowAtIndexPath method you can do:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
    cell.contentView.backgroundColor = [UIColor blueColor];
}

This saves having to retrieve your data objects again in the willDisplayCell method.

please add the following code in the cellForRowAtIndexPath

if (indexPath.row % 2 == 0){
    cell.backgroundColor =[UIColor blueColor];
} else {
    cell.backgroundColor =[UIColor whiteColor];
}

i think this will help you

The cell.contentView.backgroundColor = [UIColor colorWithRed...] method in lostInTransit's answer works, as long as you do not use the built-in label of a UITableViewCell.

I found that if you use the built-in label, eg by setting cell.text , you end up with a opaque white block under the label and only the two ends of the cell show your desired color.

I found no way to edit the built-in label so it is non-opaque (you can access it via UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0] ).

I solved the problem by adding my own custom UILabel . Like this:

UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease];
cellLabel.text = @"Test with non-opaque label";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;

[cell.contentView addSubview:cellLabel];
cell.contentView.backgroundColor = [UIColor darkGrayColor];

//time saving method:

//in cell for index method:

static NSString* evenIdentifier = @"even";
static NSString* oddIdentifier = @"odd";

__weak identifier;
if(indexPath.row %2 ==0)
{
    identifier = evenIdentifier;
}else{
    identifier = oddIdentifier;
}

cell = dequeue..WithIdentifier: identifier;
if(cell == nil){
    cell = allocOrLoadNib...;
    cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor);
}

// change the cell content and then return it. Easy job.

// this is a outline code. Please not copy it directly.

This worked for me .. In cellForRowAtIndexPath ,

cell.textLabel.backgroundColor = [UIColor clear];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want
cell.backgroundColor = cell.contentView.backgroundColor;

For you requirement, as mentioned earlier based on indexPath % 2 value you can perform the above function.

When using the default table cell setting the following two properties will color both the cell's background and the background color of it's label, avoiding the need to create a custom label as a subview of the cell's contentView.

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

This code is a slightly more clean solution for the case where you are using the built-in text label and don't want the white background color of the text label obscuring the background color. This also fixes the issue of violating the rounded corners of a grouped style of table view (which happens when you use cell.contentView.backgroundColor instead of cell.backgroundColor) :

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg;
    [bg release];

All you need to do is add the following code in your table's view controller implementation file.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
    UIColor *altCellColor = [UIColor blackColor];
    cell.backgroundColor = altCellColor;
}}

It's then as simple as adding and changing the modulus values.

Add backgroundView to the cell and set its background color of your choice.

let backgroundView = View() //  add background view via code or via storyboard
view.backgroundColor = UIColor.red // your color
cell.backgroundView = backgroundView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM