Sp0tlight
Sp0tlight

Reputation: 409

UITableView set background color

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

Upvotes: 21

Views: 51560

Answers (8)

Nikolay DS
Nikolay DS

Reputation: 1477

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
}

Upvotes: 0

Hussain Shabbir
Hussain Shabbir

Reputation: 14995

Try like this:-

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

Upvotes: 12

A.J. Hernandez
A.J. Hernandez

Reputation: 968

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

Upvotes: 1

ethemsulan
ethemsulan

Reputation: 2299

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

Upvotes: 3

Irshad Qureshi
Irshad Qureshi

Reputation: 819

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

Upvotes: 2

user3386109
user3386109

Reputation: 34829

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

Upvotes: 18

Keenle
Keenle

Reputation: 12190

  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

Upvotes: 42

NRitH
NRitH

Reputation: 13893

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

Upvotes: 1

Related Questions