UITableView Cell背景,如'Music'应用程序

时间:2012-07-18 18:58:58

标签: iphone objective-c ios xcode uitableview

  

可能重复:
  Setting background color of a table view cell on iPhone
  how to change the color alternate cell on table view

我希望有一个正常背景的单元格和下一个具有相似背景的单元格,如此截图: enter image description here 关于如何为无限数量的单元格实现这个漂亮和干净的任何想法? 提前谢谢!

1 个答案:

答案 0 :(得分:6)

FizzBu​​zz:

-(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