How to set cell background color in UITableView on iphone?

How to set cell background color in UITableView?

Thanks.

+4
source share
7 answers

I know this is an old post, but I'm sure some people are still looking for help. You can use this to set the background color of a single cell that works first:

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { [cell setBackgroundColor:[UIColor lightGrayColor]]; 

However, as soon as you start scrolling, the iphone will reuse cells that mix different background colors (if you are trying to alternate them). You need to call tableView: willDisplayCell: forRowAtIndexPath. Thus, the background color is set before loading the reuse identifier. You can do it as follows:

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = ([indexPath row]%2)?[UIColor lightGrayColor]:[UIColor whiteColor]; } 

The last line is just a compressed if / else expression. Good luck

+11
source

Update

Apparently, the existing UITableViewCell structure makes it very difficult to change the background color in a cell and works well through all its state changes (editing mode, etc.).

The solution was posted on this SO question , and it has been announced in several forums as "the only solution approved by Apple engineers." It includes a subclass of UITableViewCell and the addition of a custom view for the backgroundView subclass property.

Original mail - this solution does not work fully, but may be useful in some situations

If you already have a UITableViewCell, just change its contentView backgroundColor property.

If you need to create UITableViewCells with a custom background color, the process is a bit longer. First, you need to create a data source for your UITableView - it can be any object that implements the UITableViewDataSource protocol.

In this object, you need to implement the tableView:cellForRowAtIndexPath: method, which returns a UITableViewCell when it is provided with NSIndexPath for the location of the cell inside the table. When you create this cell, you will want to change the backgroundColor property of your contentView .

Remember to set the dataSource UITableView property to a data source object.

For more information, you can read these API docs:

Please note that registration as an Apple developer is required for all three of these links.

+2
source

The background screen is at the bottom. This is one that shows rounded corners and edges. What you want is a contentView that sits on top of the background image. It usually covers the white area of ​​the cell.

0
source

This works fine for me:

 NSEnumerator *enumerator = [cell.subviews objectEnumerator]; id anObject; while (anObject = [enumerator nextObject]) { if( [anObject isKindOfClass: [ UIView class] ] ) ((UIView*)anObject).backgroundColor = [UIColor lightGrayColor]; } 
0
source

The version I wrote will work on iPhone version 3.0 or higher, otherwise on a white background.

In your viewDidLoad UITableViewController method, add the following:

 self.view.backgroundColor=[UIColor clearColor]; // Also consider adding this line below: //self.tableView.separatorColor=[UIColor clearColor]; 

When you create your cells (in my code this is my tableView:cellForRowAtIndexPath: , add the following code:

 cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"code_bg.png"]]; float version = [[[UIDevice currentDevice] systemVersion] floatValue]; if (version >= 3.0) { [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; } 
0
source

You can set backgroundColor for backgroundView . If the backgroundView does not exist, you can create one for it.

 if (!tableView.backgroundView) { tableView.backgroundView = [[UIView alloc] initWithFrame:tableView.bounds]; } tableView.backgroundView.backgroundColor = [UIColor theMostFancyColorInTheUniverse]; 
0
source

If you want to set the cell background for the image, use this code:

  // Assign our own background image for the cell UIImage *background = [UIImage imageNamed:@"image.png"]; UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background]; cellBackgroundView.image = background; cell.backgroundView = cellBackgroundView; 
0
source

All Articles