简体   繁体   中英

How can I set the background color of a cell in UITableView on iphone?

How can I set the background color of a cell in UITableView?

Thanks.

I know this is an old post, but I am sure some people are still looking for help. You can use this to set the background color of an individiual cell, which works at first:

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

However, once you start scrolling, the iphone will reuse cells, which jumbles different background colors (if you are trying to alternate them). You need to invoke the tableView:willDisplayCell:forRowAtIndexPath. This way, the background color gets set before the reuse identfier is loaded. You can do it like this:

- (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 condensed if/else statement. Good luck!

Update

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

A solution has been posted at this SO question , and it's being billed on several forums as "the only solution approved by Apple engineers." It involves subclassing UITableViewCell and adding a custom view for the subclassed cell's backgroundView property.

Original post - this solution doesn't work fully, but may still be useful in some situations

If you already have the UITableViewCell object, just alter its contentView 's backgroundColor property.

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

In that object, you need to implement the tableView:cellForRowAtIndexPath: method, which returns a UITableViewCell when given an NSIndexPath for the location of the cell within the table. When you create that cell, you'll want to change the backgroundColor property of its contentView .

Don't forget to set the dataSource property of the UITableView to your data source object.

For more info, you can read these API docs:

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

This works perfectly for me:

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

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

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

The version I wrote will work in iPhone 3.0 or higher and fallback to a white background otherwise.

In your viewDidLoad method of the UITableViewController we add the following:

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

When you are creating 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]];
}

If you want to set the background of a cell to an image then 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;

The backgroundView is all the way on the bottom. It's the one that shows the rounded corners and the edges. What you want is the contentView which is on top of the backgroundView. It covers the usually white area of the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM