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!

4

1 に答える 1

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

于 2012-07-18T19:05:37.450 に答える