UITableView set background color

50,243

Solution 1

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;
}

Solution 2

  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

Solution 3

Try like this:-

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

Solution 4

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()
}

Solution 5

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()
        }
}
Share:
50,243

Related videos on Youtube

Sp0tlight
Author by

Sp0tlight

Updated on July 09, 2022

Comments

  • Sp0tlight
    Sp0tlight almost 2 years

    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

    • Hussain Shabbir
      Hussain Shabbir almost 10 years
      But why are you displaying empty row??
    • Sp0tlight
      Sp0tlight almost 10 years
      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.
    • misnomer
      misnomer almost 9 years
      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.
  • Sp0tlight
    Sp0tlight almost 10 years
    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
    Sp0tlight almost 10 years
    So something like providing a custom view as the background view of the UITableView?
  • Keenle
    Keenle almost 10 years
    To manage tableview's background you can work with self.tableView.backgroundView.layer. Here is a good tutorial: raywenderlich.com/2502/…
  • Sp0tlight
    Sp0tlight almost 10 years
    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).
  • Andy Weinstein
    Andy Weinstein over 7 years
    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
  • Irshad Qureshi
    Irshad Qureshi over 7 years
    Welcome Brother