UITableView set background color
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
- Open Storyboard
- Select your UITableView
- Open Attribute inspector
- Scroll to View group
- Select background color for entire table.
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()
}
}
Related videos on Youtube
Sp0tlight
Updated on July 09, 2022Comments
-
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?
-
Hussain Shabbir almost 10 yearsBut why are you displaying empty row??
-
Sp0tlight almost 10 yearsI 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 almost 9 yearsIf 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 almost 10 yearsThank 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 almost 10 yearsSo something like providing a custom view as the background view of the UITableView?
-
Keenle almost 10 yearsTo manage tableview's background you can work with
self.tableView.backgroundView.layer
. Here is a good tutorial: raywenderlich.com/2502/… -
Sp0tlight almost 10 yearsThank 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 over 7 yearsThis 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 over 7 yearsWelcome Brother