21

I change the background color of the UITableViewCells in the tableView:cellForRowAtIndexPath method

    if(indexPath.row % 2 == 0){
        cell.backgroundColor = ...
    } else{
        cell.backgroundColor = ...
    }

But that only change the color of the cell amount specified in tableView:numberOfRowsInSection (As seen in the attached picture, there are white cells after the first four)

Is it possible to change the color of all cells that are displayed on screen?

enter image description here

3
  • But why are you displaying empty row?? Jul 14, 2014 at 18:48
  • I think thats the default behavior of the tableview to display as many rows as the height of the screen? and it looks kind of strange if only a few datasets are displayed to the user, so the empty ones are white.
    – Sp0tlight
    Jul 14, 2014 at 18:53
  • If you add an empty footer view, then the extra rows will disappear and give you one solid colour for the rest of the rows.
    – misnomer
    May 25, 2015 at 18:57

8 Answers 8

42
  1. Open Storyboard
  2. Select your UITableView
  3. Open Attribute inspector
  4. Scroll to View group
  5. Select background color for entire table.

enter image description here

5
  • Thank you, this looks way better right now. But could I also achieve the same style as the table (different colors for odd and even cells) or is this not possible by default?
    – Sp0tlight
    Jul 14, 2014 at 18:39
  • So something like providing a custom view as the background view of the UITableView?
    – Sp0tlight
    Jul 14, 2014 at 18:45
  • To manage tableview's background you can work with self.tableView.backgroundView.layer. Here is a good tutorial: raywenderlich.com/2502/…
    – Keenle
    Jul 14, 2014 at 18:49
  • Thank you, I ended up using the solution suggested by user3386109. But CALayers are really interesting. They helped me to solve an other problem I had (add a drop-shadow to the header of the section in the TableView).
    – Sp0tlight
    Jul 14, 2014 at 19:16
  • 1
    This does not work for me. Did this on a cell by cell basis in cellForRowAtIndexPath as per user3386109. Maybe the table is covered by all of its cells or something like that, so I am never seeing the actual table background. Not sure what the case is where this works Aug 9, 2016 at 9:26
18

If you want the cell background color to continue to alternate, then you need to lie about how many rows are in the table. Specifically, in tableView:numberOfRowsInSection you need to always return a number that will fill the screen, and in tableView:cellForRowAtIndexPath, return a blank cell for rows that are beyond the end of the table. The following code demonstrates how to do this, assuming that self.dataArray is an NSArray of NSStrings.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ( self.dataArray.count < 10 )
        return( 10 );
    else
        return( self.dataArray.count );
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCell"];

    if ( indexPath.row % 2 == 0 )
        cell.backgroundColor = [UIColor orangeColor];
    else
        cell.backgroundColor = [UIColor redColor];

    if ( indexPath.row < self.dataArray.count )
        cell.textLabel.text = self.dataArray[indexPath.row];
    else
        cell.textLabel.text = nil;

    return cell;
}
12

Try like this:-

self.tableView.backgroundView.backgroundColor = [UIColor blueColor];
3

In swift, you can change tableview background color or you can set image to tableview background color like this;

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
    self.tableView.backgroundColor = UIColor.clearColor()
}


//    change cell text color and background color
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = UIColor.clearColor()
}
0
2

I used this to color alternate cell in a table view

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) {

        if (indexPath.row % 2 == 0)
        {
            cell.backgroundColor = UIColor.grayColor()
        }
        else
        {
            cell.backgroundColor = UIColor.whiteColor()
        }
}
1

You need to set the tableview's backgroundView to nil and its backgroundColor to the desired color.

1

For SWIFT

Thanks to @Irshad Qureshi I was able to get alternating background colors for my prototype cells by adding the following in my cellForRowAtIndexPath:

if (indexPath.row % 2 == 0)
    {
        cell!.backgroundColor = UIColor.groupTableViewBackgroundColor()
    }
    else
    {
        cell!.backgroundColor = UIColor.whiteColor()
    }
1
0

Swift 5 and up

 override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    //Set Clear Backcolor
    //cell.backgroundColor = .clear
    //or in your case
    if ( indexPath.row % 2 == 0 )
        cell.backgroundColor = .orange
    else
        cell.backgroundColor = .red
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.