2011-05-21 114 views
2

我有一个没有XIB的UITableview控制器。 我通过这样设置的tableview的背景色:UITableview背景颜色-iphone

[self.tableView setBackgroundColor:[UIColor colorWithPatternImage: 
            [UIImage imageNamed:@"MmyImage.png"]]]; 

我的图像是一个梯度图像,所以eventhough上面的代码是好的,它重绘图像针对每个小区时的tableview进入编辑视图,它看起来不像看起来优雅的线条。

我想设置tableviews背景以清除颜色并将超级视图的颜色设置为图像,以便tableview在超级视图上平滑过渡。但下面的代码不起作用:

[self.view setBackgroundColor:[UIColor colorWithPatternImage: 
           [UIImage imageNamed:@"myImage.png"]]]; 
[self.tableView setBackgroundColor:[UIColor clearColor]]; 

这使得背景完全白色,我不知道为什么。

帮助赞赏..谢谢。

编辑:新的代码,但是,这并不工作,以及:

UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]]; 
    imgBg.frame = self.tableView.window.bounds; 
    [self.tableView.superview addSubview:imgBg]; 
//[self.view addSubview:imgBg]; 
    //[self.tableView.window sendSubviewToBack:imgBg]; 
    //[self.view bringSubviewToFront:self.tableView]; 
    [self.tableView setBackgroundColor:[UIColor clearColor]]; 
    [imgBg release]; 
+1

你试过了:'self.tableView.opaque = NO;'? – 2011-05-21 09:00:00

+0

细胞的背景也可能是白色的。 – 2011-05-21 09:03:41

+0

尝试不透明,不起作用..细胞背景有一个单独的灰色渐变图像,它工作正常.. – user542584 2011-05-21 09:45:22

回答

6

在自定义UITableViewController子类中,self.view将返回与self.tableView相同的值。所以你在那里做的是改变同一个对象。最后,你会看到UIWindow。因此,要获得期望的结果,你应该这样做 -

[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]]; 
[self.tableView setBackgroundColor:[UIColor clearColor]]; 

UITableViewbackgroundView属性。用它。

aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease]; 
aTableView.backgroundView.backgroundColor = backgroundColor; 
+0

嗨,感谢您的建议,尝试过,但不起作用...它显示纯白色作为背景与上述。 – user542584 2011-05-21 09:45:53

+0

编辑我的答案。它为我工作。 – 2011-05-21 10:08:58

+0

谢谢,它终于起作用了! – user542584 2011-05-21 10:31:34

0

那么你的第二个代码将是正确的,除了:

[self.view setBackgroundColor:[UIColor colorWithPatternImage: 
           [UIImage imageNamed:@"myImage.png"]]]; 

替换此:

UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]]; 
imgBg.frame = self.view.bounds; 
[self.view addSubview:imgBg]; 
[self.view sendSubviewToBack:imgBg]; 

我认为: [self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]];会有所帮助,这是现在唯一的区别。

+0

我有单元格背景颜色设置好。我需要的是桌面背景颜色。 – user542584 2011-05-21 09:46:19

+0

我已编辑我的答案,以您的需求 – Roki 2011-05-21 10:03:56

+0

嗨,谢谢,但仍然无法正常工作。我用我的最新代码编辑了我的答案。我也尝试了评论中的选项。 – user542584 2011-05-21 10:26:11

0

首先在你的xib文件中添加一个父视图。在该父视图中添加表视图。将父视图的背景颜色设置为所需的背景。然后将你的tableview的背景颜色设置为[UIColor clearColor]。

如果您的表视图是组表样式或不是?如果它是组表格样式,那么该单元格具有不同的背景。然后你需要清除每个单元格的背景。做最好的办法是,

  1. 创建一个空白UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];

  2. 集这 为你的细胞背景视图。

我希望这会起作用。

+0

嗨,正如我所说,没有XIB ..所以不幸我无法编辑它。 – user542584 2011-05-21 10:27:33