Setting background color of a table view cell on iPhone

后端 未结 12 706
孤街浪徒
孤街浪徒 2020-12-02 11:23

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

相关标签:
12条回答
  • 2020-12-02 12:01

    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.

    0 讨论(0)
  • 2020-12-02 12:02

    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

    0 讨论(0)
  • 2020-12-02 12:02

    //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.

    0 讨论(0)
  • 2020-12-02 12:05

    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, e.g. 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];
    
    0 讨论(0)
  • 2020-12-02 12:06

    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
    
    0 讨论(0)
  • 2020-12-02 12:12

    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
    
    0 讨论(0)
提交回复
热议问题