title: iOS UITableView: changing background color of table overrides background color of cells
body: <p>I have an issue where setting the background color of my tableView overrides the background colors I've set for my individual cells. To be more specific, I have a table with 3 rows: the top cell is dark gray, and the bottom 2 cells are red:</p>
  
  <p><a href="http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#0" rel="nofollow">http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#0</a></p>
  
  <pre><code>- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  {
      UIColor *sectionBackgroundColor = [UIColor colorWithRed:0.09f green:0.09f blue:0.09f alpha:1.0];
      UIColor *menuItemBackgroundColor = [UIColor redColor];
  
      if (indexPath.row == 0) {
          cell.backgroundColor = sectionBackgroundColor;
          cell.textLabel.textColor = [UIColor whiteColor];
          cell.textLabel.text = @"MENU";
          cell.textLabel.font= [UIFont systemFontOfSize:16.0];
          cell.selectionStyle = false;
      } else if (indexPath.row == 1) {
          cell.backgroundColor = menuItemBackgroundColor;
          cell.textLabel.textColor = [UIColor whiteColor];
          cell.textLabel.text = @"Schedule";
          cell.textLabel.font= [UIFont systemFontOfSize:18.0];
          cell.imageView.image = [UIImage imageNamed:@"Schedule.png"];
      } else if (indexPath.row == 2) {
          cell.backgroundColor = menuItemBackgroundColor;
          cell.textLabel.textColor = [UIColor whiteColor];
          cell.textLabel.text = @"Contacts";
          cell.textLabel.font= [UIFont systemFontOfSize:18.0];
          cell.imageView.image = [UIImage imageNamed:@"Contacts.png"];
      } else {
          cell.backgroundColor = [UIColor blackColor];
          cell.selectionStyle = false;
      }
  
  }
  </code></pre>
  
  <p>What I'm trying to do is make all those empty, unused white cells on the bottom black. Based on my research, setting the tableView background color will do just that:</p>
  
  <p><a href="http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#1" rel="nofollow">http://imgur.com/otYlFMx,LvoLDzY,tXiJenw#1</a></p>
  
  <pre><code>tableView.backgroundColor = [UIColor blackColor];
  </code></pre>
  
  <p>However, as you noticed, this line of code changes the background colors of the top two cells to black too. I played around with this more and noticed a general pattern where every cell background color will be changed to black except the last one:</p>
  
  <p><em>(see 3rd image in the above link)</em></p>
  
  <p>Any ideas regarding what's happening here? Help would be appreciated!</p>
  
  <hr>
  
  <hr>
  
  <p>Some things I've tried already that do not work (I would provide more links but my current rep doesn't allow me to):</p>
  
  <p>A) Making the views in the cells opaque. Changing the colors of the different views in the cells.</p>
  
  <pre><code>cell.opaque = YES;
  cell.backgroundView.opaque = YES;
  cell.contentView.opaque = YES;
  cell.backgroundColor = menuItemBackgroundColor;
  cell.contentView.backgroundColor = menuItemBackgroundColor;
  cell.backgroundView.backgroundColor = menuItemBackgroundColor;
  </code></pre>
  
  <p>B) Setting the backgroundView of tableView nil.</p>
  
  <pre><code>tableView.backgroundView = nil;
  </code></pre>
  
  <p>C) Setting the background color of the backgroundView of tableView.</p>
  
  <pre><code>tableView.backgroundView.backgroundColor = [UIColor blackColor];
  </code></pre>