简体   繁体   中英

Change each UITableViewCell background color in iPhone app?

Am working for an iPhone app with using UITableView . In my tableview i have multiple sections with multiple cells. I want to give the cell background color for each cell. That means each cell should have different colors .

Ex: If there is 20 cells means each 20 cells should have a different cell background colors.

Can you please tell me if it is possible to give cell background color/image to each cell? Can you please guide me to do this?

Thanks in advance.

EDIT

My Tableviewcell color will be like below,

First Cell background color : white color
Second Cell background color : red color
Third Cell background color : orange color
Fourth cell background color : gray color
Fifth Cell background color : white color
Sixth cell background color : white color
Seventh cell background color : white color
Eight cell background color : gray color
Ninth cell background color : red color
Tenth cell background color : red color

I want to give the tableview cell background color like this. Then the background color will be change when the UITableView reload or newly opening. Can you please help me to do like this? It is possible to do in iPhone app tableview?

Thanks in advance.

You actually can't use the standard tableView:cellForRowAtIndexPath: workflow to do this. The background color of a cell is a special case because the table view will modify it while managing the cells' selected states.

Instead, as decribed in this answer you must implement the tableView:willDisplayCell:atIndexPath: method. There is an old WWDC video which covers this, I think from the 2009 or 2010 session.

For example, you could do something like this:

- (void)tableView:(UITableView*)tableView 
  willDisplayCell:(UITableViewCell*)cell 
forRowAtIndexPath:(NSIndexPath*)indexPath
{
    cell.backgroundColor = /* random color here */;
}

try this

- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    double numSections = [self numberOfSectionsInTableView:tableView];
    double numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section];
    [cell setBackgroundColor:[UIColor colorWithRed:0.8/numRows*((double)indexPath.row)+0.2 green:0.8/numSections*((double)indexPath.section)+0.2 blue:((double)(random()%1000))/1000.0 alpha:1]];
    return cell;
}

It's a bit complex, but here's how to do it in cellForRowAtIndexPath. Just paste this code into your method. I wrote it according to your specifications above.

if (indexPath.row == 0 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6) {
            [cell.textLabel setBackgroundColor:[UIColor whiteColor]];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            [cell.textLabel setTextColor:[UIColor blackColor]];
        } else {
            if (indexPath.row == 1 || indexPath.row == 8 || indexPath.row == 9){
                 [cell.textLabel setBackgroundColor:[UIColor redColor]];
                 cell.contentView.backgroundColor = [UIColor redColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
            } else {
                  if (indexPath.row == 3 || indexPath.row == 7){
                 [cell.textLabel setBackgroundColor:[UIColor grayColor]];
                 cell.contentView.backgroundColor = [UIColor grayColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
                 } else {
                      [cell.textLabel setBackgroundColor:[UIColor orangeColor]];
                      cell.contentView.backgroundColor = [UIColor orangeColor];
                      [cell.textLabel setTextColor:[UIColor blackColor]];
}}}

There are, of course, other ways to do this (such as using switch , or by having a declared UIColor, and changing its value according to which indexPath.row is being called). But after implementing, you can simplify it yourself.

Note that it is necessary to set the background color of both the textLabel and the contentView, if you want uniformity.

Cheers!

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