0

Possible Duplicate:
Setting background color of a table view cell on iPhone
how to change the color alternate cell on table view

I want to have one cell with normal background and the next one with somewhat great background like in this screenshot: enter image description here Any ideas in how to implement this nice and clean for an unlimited number of cells? Thanks in advance!

Community
  • 1
  • 1
Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • 1
    That is a "great" background...What have you tried? How about these:http://stackoverflow.com/questions/10213364/how-to-change-the-backgroundcolor-for-all-uitableviewcells http://stackoverflow.com/questions/900094/setting-background-color-of-a-table-view-cell-on-iphone?lq=1 http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell The second one is pretty much exactly your question. In fact, this is basically a duplicate. – Justin Paulson Jul 18 '12 at 19:03

1 Answers1

6

FizzBuzz:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if ((indexPath.row % 2) == 0) {
        [cell.contentView setBackgroundColor:[UIColor whiteColor]];
        [cell.textLabel setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [cell.contentView setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
        [cell.textLabel setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];
    }
    cell.textLabel.text = [songArray objectAtIndex:indexPath.row];
    return cell;
}

enter image description here

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • This approach is fine unless you have any editing accessory in the cell. This code I tried `[cell.accessoryView setBackgroundColor:[UIColor colorWithRed:243.0f/255.0f green:243.0f/255.0f blue:243.0f/255.0f alpha:1.0f]];` isn't working – Sergey Grischyov Jul 19 '12 at 09:07