0

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.

Gopinath
  • 5,392
  • 21
  • 64
  • 97
  • Possible duplicate of http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell –  May 07 '12 at 13:25

3 Answers3

0

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 */;
}
Community
  • 1
  • 1
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • Actually, you CAN do it in `cellForRowAtIndexPath`, it's just kind of complicated. I will post an answer showing how. – AMayes May 07 '12 at 16:07
  • Sorry, but this is a very well-known limitation which Apple engineers have covered in at least one WWDC video. Using cellForRowAtIndexPath is not reliable and you will get strange behaviour and inconsistent results. The only reliable and officially supported way to configure the cell background color is using willDisplayCell. – Mike Weller May 08 '12 at 06:10
0

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;
}
Jason McTaggart
  • 878
  • 1
  • 7
  • 18
0

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!

AMayes
  • 1,767
  • 1
  • 16
  • 29
  • 1
    Unless you want random coloring. Then, this is completely the wrong way to go about it lol. – AMayes May 07 '12 at 16:27