在xcode的表视图中为备用单元格赋予颜色

时间:2011-05-30 08:37:08

标签: iphone cocoa-touch xcode ipad uitableview

我想为表格视图单元格提供替代颜色。例如,第一个单元格颜色保持白色,而下一个单元格是浅灰色,然后第三个单元格再次变白,然后接下来又是浅灰色等等。任何人都可以告诉我这样做的方法。

谢谢, 克里斯蒂

5 个答案:

答案 0 :(得分:8)

使用这个christy:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.row == 0 || indexPath.row%2 == 0) {
        UIColor *altCellColor = [UIColor colorWithRed:100 green:10 blue:10 alpha:1];
        cell.backgroundColor = altCellColor;
    }
}

答案 1 :(得分:4)

在您的cell:(UITableviewCell*) forRowAtIndexPath:(NSIndexPath*) indexPath数据源方法中插入此测试:

NSUinteger row=indexPath.row;
if (row % 2==0)
 cell.contentView.backGroundColor=[UIColor blueClor] ;
else
 cell.contentView.backGroundColor=[UIColor whiteClor] ;

答案 2 :(得分:2)

Hii Christina:

检查一下,

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ((indexPath.row % 2) == 0) {

    cell.backgroundColor = [UIColor whiteColor];

            }
    else {

    cell.backgroundColor = [UIColor lightgrayColor];
        }
}

希望这有帮助!

答案 3 :(得分:2)

创建UITableViewCell时使用以下代码。

if(indexPath.row % 2 == 0)
{
    myCell.backgroundColor = [UIColor whiteColor];
}
else if(indexPath.row % 2 == 1)
{
    myCell.backgroundColor = [UIColor grayColor];
}

答案 4 :(得分:2)

以非常通用的方式尝试这个Christy:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{
cell.backgroundColor = indexPath.row % 2 ? [UIColor lightGrayColor] : [UIColor whiteColor];
}
相关问题