设置UITableViewCell和UITableView背景的颜色

问题描述:

我想为我的TableView使用背景图片,所以当没有足够的数据填充屏幕上的整个表格时,我没有得到剩余的tableview单元格,所以我试图按照这个网址,但要简单得多:http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html设置UITableViewCell和UITableView背景的颜色

我这样做是有一个出口我的UITableView的视图控制器:

UIImage *backgroundTableImage = [UIImage imageNamed:@"form_background.png"]; // basically a gray->white gradient 
    UIImageView *tableBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundTableImage]; 
    tableBackgroundImageView.frame = self.TableView.bounds; 
    self.TableView.backgroundView = tableBackgroundImageView; 
    [tableBackgroundImageView release]; 
    self.backgroundColor = [UIColor clearColor]; 

然后我的UITableViewCell的自定义子类,我在IB创建。在IB中,它的背景设置为白色默认值。我的细胞都是透明的,我只能看到我的背景。我如何让我的细胞保持白色。

+0

这行`tableBackgroundImageView.frame = self.TableView.bounds`似乎不正确。你确定这是做你想做的吗? – gurooj 2011-12-15 01:30:11

首先在你的.h文件中查看UIView * newView;申报其财产和的cellForRowAtIndexPath

if(cell==nil){ 

     tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imagename.png"]]; 

................ 
......... 
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)]; 
[cell.contentView addSubview:newView]; 
................ 
............... 
} 
if(indexPath.row == 0){ 

newView.backgroundColor = [UIColor blueColor]; 

} 
if(indexPath.row == 1){ 

newView.backgroundColor = [UIColor redColor]; 

} 
if(indexPath.row == 2){ 

newView.backgroundColor = [UIColor greenColor]; 

} 

在您.m文件

synhesize它现在如果还有问题,请把我打倒。