James Chen
James Chen

Reputation: 407

How would I programmatically change the background color of a cell

So my actual question is a bit more robust. I'm curious whether it's possible to change the background color of a cell programmatically, however it would be based on whether the cell was first, or second, etc.

I'm not sure this is even possible, but what I'm trying to achieve is a gradient effect utilizing the cells as they increase in number.

if (indexPath.row % 2)
    {
        cell.contentView.backgroundColor = UIColor.redColor()
    }
    else
    {
        cell.contentView.backgroundColor = UIColor.blueColor()
    }

I've tried something like this to at least have the color alternate to see if I can figure out what to do next, but this has failed. Alternating doesn't seem to be the right option, but it might be the right way to start programming what I want to happen.

Upvotes: 3

Views: 6272

Answers (3)

Ganesh Pawar
Ganesh Pawar

Reputation: 191

Swift 4.2

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if indexPath.row % 2 == 0 {

         cell.backgroundColor = UIColor.red
         //OR
         productCell.contentView.backgroundColor = UIColor.red
    }
    else {

         cell.backgroundColor = UIColor.blue
    }

    return cell
}

Upvotes: -1

Mayank Patel
Mayank Patel

Reputation: 3908

A tableView sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. for more info

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row % 2 == 0 {
         cell.backgroundColor = UIColor.redColor()
    }
    else {
         cell.backgroundColor = UIColor.blueColor()
    }
    return cell
}

Upvotes: 7

pkc456
pkc456

Reputation: 8506

Yes, it is possible to change the background color of a cell programmatically, based on whether the cell was first, or second, etc. In cellForRowAtIndexPath delegate method, check if row is odd then put the different background color, if it is even then put the different color. By using below code, you will get alternate colors of rows.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommentTableCellIdentifier"];
    cell.textLabel.text = @"Your text goes here";
    if(indexPath.row % 2 == 0)
        cell.backgroundColor = [UIColor redColor];
    else
        cell.backgroundColor = [UIColor greenColor];
    return cell;
}

SWIFT:-

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    // Configure the cell...
    let cellId: NSString = "Cell"
    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    cell.textLabel.text = "Your text goes here"
    if(indexPath.row % 2 == 0)
    {
        cell.backgroundColor = UIColor.redColor()
    }
    else
    {
        cell.backgroundColor = UIColor.greenColor()
    }
    return cell
}

Upvotes: 0

Related Questions