2009-05-22 147 views
33

我想要实现的效果是,表格视图中的一个单元格将具有蓝色背景,下一个单元格将具有白色,下一个单元格会再次呈现蓝色,然后是白色,等等......你能让我知道我该怎么做?在iPhone上设置表格视图单元格的背景颜色

谢谢。

+0

可能的重复:http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcell – ilhnctn 2012-07-10 09:31:17

+1

@willcodejavaforfood这是一个非常无益的评论,是不相关的OP问题 – Max 2015-05-15 13:14:09

回答

39

必须设置单元格的内容视图的背景颜色

cell.contentView.backgroundColor = [UIColor colorWithRed...] 

这将设置整个单元格的背景。

要为备用电池做到这一点,在lostInTransit的答案作品使用indexPath.row%由2

+0

感谢您的回复。我曾尝试过,但它只显示了单元格的两端为蓝色/橙色,但我希望整个单元格具有颜色 – ebaccount 2009-05-25 17:05:37

+2

如果使用cell.backgroundColor,则会以蓝色/橙色显示两端。如果使用cell.contentView.backgroundColor,则整个单元格将被着色,除非在单元格中具有任何具有白色背景的控件(如标签)。在这种情况下,你将不得不改变他们的背景颜色。否则contentView.backgroundColor方法一直工作到现在。 – lostInTransit 2009-05-26 06:12:14

+0

我来这里寻找如何改变整个单元格的背景颜色,但是发布的问题并不完全相关,但是这个答案对我来说很有用,谢谢 – 2013-04-28 08:58:51

4

cell.contentView.backgroundColor = [UIColor colorWithRed...]方法,只要你不使用内置的一个UITableViewCell的标签。

我发现如果您使用内置标签,例如通过设置cell.text,您最终将在标签下方显示一个不透明的白色块,并且只有单元格的两端显示所需的颜色。

我发现没有办法编辑内置标签,因此它是不透明的(您可以通过UILabel* cellLabel = [cell.contentView.subviews objectAtIndex:0]访问它)。

我通过添加自己的自定义UILabel来解决问题。就像这样:

UILabel* cellLabel = [[[UILabel alloc] initWithFrame:cell.frame] autorelease]; 
cellLabel.text = @"Test with non-opaque label"; 
cellLabel.backgroundColor = [UIColor clearColor]; 
cellLabel.opaque = NO; 

[cell.contentView addSubview:cellLabel]; 
cell.contentView.backgroundColor = [UIColor darkGrayColor]; 
2

该代码的情况下的稍微干净的解决方案,您使用的是内置的文本标签和不希望的文本标签模糊的背景色的白色背景颜色。这也修复违反表视图的组合风格的圆角的问题(当你使用cell.contentView.backgroundColor代替cell.backgroundColor这恰好):

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
cell.backgroundColor = [UIColor redColor]; 
cell.textLabel.backgroundColor = [UIColor clearColor]; //transparent background 
73

添加这个方法将你的表视图委托:

#pragma mark UITableViewDelegate 
- (void)tableView: (UITableView*)tableView 
    willDisplayCell: (UITableViewCell*)cell 
forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = indexPath.row % 2 
     ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
     : [UIColor whiteColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
} 
4

请添加以下代码中的cellForRowAtIndexPath

if (indexPath.row % 2 == 0){ 
    cell.backgroundColor =[UIColor blueColor]; 
} else { 
    cell.backgroundColor =[UIColor whiteColor]; 
} 

我认为这将有助于你

3

使用默认表格单元格设置时,以下两个属性将着色单元格的背景和其标签的背景颜色,从而避免需要创建自定义标签作为单元格contentView的子视图。

cell.textLabel.backgroundColor = [UIColor redColor]; 
cell.contentView.backgroundColor = [UIColor redColor]; 
9

如果您想根据实际单元格的数据对象的一些状态设置单元格颜色,那么这是另一种方法:

如果此方法添加到您的表视图代表:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
cell.backgroundColor = cell.contentView.backgroundColor; 
} 

然后在你的cellForRowAtIndexPath方法,你可以这样做:

if (myCellDataObject.hasSomeStateThatMeansItShouldShowAsBlue) { 
    cell.contentView.backgroundColor = [UIColor blueColor]; 
} 

这节省了RET的在willDisplayCell方法中再次删除数据对象。

3

//节省时间的方法:

//在索引方法细胞:

static NSString* evenIdentifier = @"even"; 
static NSString* oddIdentifier = @"odd"; 

__weak identifier; 
if(indexPath.row %2 ==0) 
{ 
    identifier = evenIdentifier; 
}else{ 
    identifier = oddIdentifier; 
} 

cell = dequeue..WithIdentifier: identifier; 
if(cell == nil){ 
    cell = allocOrLoadNib...; 
    cell.backgroundColor = (indexPath.row %2 ==0 ? evenColor : oddColor); 
} 

//更改单元格的内容,然后返回。轻松的工作。

//这是一个大纲代码。请勿直接复制。

3

这为我工作。在的cellForRowAtIndexPath,

cell.textLabel.backgroundColor = [UIColor clear]; 
cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
cell.contentView.backgroundColor = [UIColor grayColor]; //whichever color u want 
cell.backgroundColor = cell.contentView.backgroundColor; 

对于你的要求,如前面基于indexPath%2的值就可以执行上述功能提到。

0
UIView *bg = [[UIView alloc] initWithFrame:cell.frame]; 
    bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
    cell.backgroundView = bg; 
    [bg release]; 
0

您只需在表格的视图控制器实施文件中添加以下代码即可。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row%2 == 0) { 
    UIColor *altCellColor = [UIColor blackColor]; 
    cell.backgroundColor = altCellColor; 
}} 

然后就像添加和更改模数值一样简单。

相关问题