2010-02-22 150 views
2

如何填充UITableViewCell的背景颜色?我试过这段代码,但没有奏效。如何填充UITableViewCell的背景颜色?

cell.backgroundColor = [UIColor redColor]; 
+1

对此不起作用?你把那些代码放在哪里?请更多信息! –

+0

这是一个愚蠢的http://stackoverflow.com/questions/400965/how-to-customize-the-background-border-colors-of-a-grouped-table-view? –

+0

感谢Mike Akers – RAGOpoR

回答

2

尝试设置backgroundView为UITableViewCell

UIView *bgView = [[UIView alloc] init]; 
bgView.backgroundColor = [UIColor redColor]; 
cell.backgroundView = bgView; 
// release object 
[bgView release]; 

您可以更改的UITableViewCell用同样的方法选择背景图。

cell.selectedBackgroundView = bgView; 
+0

感谢您的代码syd – RAGOpoR

+0

这是最好的 - 我发现其他解决方案没有奏效。谢谢。 – jocull

1

设置背景颜色虽然UITableViewCellbackgroundColor属性只在分组表视图工作。所以如果你的表格视图在Plain风格,那么它将不起作用。

你当然可以设置UITableViewcontentView的背景颜色。但是,那么你可能不得不做一些额外的工作,因为其他子视图(文本标签和附件视图)对背景颜色有自己的想法。

1

查看下面的UITableViewCell文档片段。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html

注意:如果你想改变一个单元格的背景色(通过经由的UIView宣布backgroundColor属性设置单元格的背景颜色),你必须这样做在的tableView:willDisplayCell :forRowAtIndexPath:委托的方法,不在tableView:cellForRowAtIndexPath中:数据源。在iOS 3.0中,对组合式表格视图中单元格的背景颜色的更改与以前版本的操作系统不同。它现在会影响圆角矩形内的区域,而不是其外部的区域。

这应该可以解决您的问题。