how to set different background colors for cells in a UITableView (specifically rainbow color for seven cells)
Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.
you can just change the backgroundView's backgroundColor property like so: cell. selectedBackgroundView?. backgroundColor = <your color .
The UITableViewCell class allows developers to assign a custom background view using the “backgroundView:” method. To assign your own custom background image, we can create an UIImageView object with the background image and set it as the background view of the cell. Add the following code to the “cellForRowAtIndexPath:” method:
For the standard types, UITableViewCell provides the views for displaying your content. All you have to do is assign values to the textLabel, detailTextLabel, and imageView properties of your cell. The following illustration shows how the values you supply are positioned within the cell’s content area.
Inside the cellForRowAt indexPath method, on the table view cell, set the selectionStyle to .none. It is as easy as that to remove the highlight/selection color from a UITableViewCell. One could make a very simple extension method that can make this a bit easier in terms of autocomplete:
Table View Cell selection background color can be set via the Storyboard in Interface Builder: If you have a grouped table with just one cell per section, just add this extra line to the code: bgColorView.layer.cornerRadius = 10; Don't forget to import QuartzCore. Swift 3: for me it worked when you put it in the cellForRowAtIndexPath: method
Set the backgroundColor
property:
cell.backgroundColor = [UIColor redColor];
Note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath:
method (from UITableViewCell reference):
Note: If you want to change the background color of a cell (by setting the background color of a cell via the backgroundColor property declared by UIView) you must do it in the tableView:willDisplayCell:forRowAtIndexPath: method of the delegate and not in tableView:cellForRowAtIndexPath: of the data source. Changes to the background colors of cells in a group-style table view has an effect in iOS 3.0 that is different than previous versions of the operating system. It now affects the area inside the rounded rectangle instead of the area outside of it.
Use the indexPath parameter to achieve the rainbow effect.
If you want to set cell color based on some state in the actual cell data object, then this is another approach:
If you add this method to your table view delegate:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = cell.contentView.backgroundColor;
}
Then in your cellForRowAtIndexPath method you can do:
if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) {
cell.contentView.backgroundColor = [UIColor blueColor];
}
This saves having to retrieve your data objects again in the willDisplayCell method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With