Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell background color

Tags:

how to set different background colors for cells in a UITableView (specifically rainbow color for seven cells)

like image 300
Shabbir Panjesha Avatar asked May 07 '11 13:05

Shabbir Panjesha


People also ask

How to change UITableView background color?

Select the Tableview. Go to the Attributes Inspector. Set the Tableview Background Colour. Set the View Background Colour.

How do I change the selection color of Uitableviewcell in iOS?

you can just change the backgroundView's backgroundColor property like so: cell. selectedBackgroundView?. backgroundColor = <your color .

How do I add a background image to a uitableviewcell?

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:

How do you display content in uitableviewcell?

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.

How to remove highlight/selection color from a uitableviewcell?

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:

How to change the background color of a cell in Tableview?

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


2 Answers

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.

like image 184
albertamg Avatar answered Oct 01 '22 14:10

albertamg


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.

like image 36
gamozzii Avatar answered Oct 01 '22 16:10

gamozzii