2011-05-30 96 views
3

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

感谢, 克里斯蒂

回答

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] ; 
8

使用这种克里斯汀:

- (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; 
    } 
} 
2

斐伊川克里斯蒂娜:

检查了这一点,

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

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

    cell.backgroundColor = [UIColor whiteColor]; 

      } 
    else { 

    cell.backgroundColor = [UIColor lightgrayColor]; 
     } 
} 

希望这有助于!

2

当您创建UITableViewCell时,请使用以下代码。

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

我认为你缺少一个 '='。应该是'if(indexPath.row%2 == 0)'等 – 2013-08-19 14:44:12

+0

是的,谢谢指出。 – Jhaliya 2013-08-20 04:38:03

2

在很多通用的方式试试这个克里斯蒂:

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