iPhone:分组TableView背景图像问题

iPhone:分组TableView背景图像问题,iphone,background,uitableview,Iphone,Background,Uitableview,大家好,这看起来应该很简单;我真的希望是这样。一如既往,提前感谢 我所要做的就是将背景图像添加到分组样式的tableview中。我在viewDidLoad中使用以下方法: self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; UIView *bgView = [[UIView alloc] initWithFrame:CGRectM

大家好,这看起来应该很简单;我真的希望是这样。一如既往,提前感谢

我所要做的就是将背景图像添加到分组样式的tableview中。我在viewDidLoad中使用以下方法:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.tableView.window addSubview:bgView];

    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];
UIImage *background = [UIImage imageNamed:@"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];
不幸的是,background.png似乎不仅绘制在单元格后面,还绘制在单元格和标题的背景上,如下所示:

我知道在这个网站上有一些问题可以解决类似的问题,但我担心我无法得到任何工作。让我补充说

self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
查看、加载和添加

cell.backgroundView = nil;
cell.opaque = NO;
在配置我的单元格时。不用说,它不起作用。添加

cell.backgroundColor = [UIColor clearColor];
也不管用。可悲的是,这是我最后的想法。如果可能的话,我真的希望能够做到这一点,而无需在interface builder中添加背景。我喜欢尽可能多地用编程的方式来做。我希望这样问不会太贪婪。再次感谢

* * * 编辑: * * *

我遇到了实现这一点的另一种方法,但遇到了另一个问题。我向viewDidLoad添加了以下代码:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.tableView.window addSubview:bgView];

    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];
UIImage *background = [UIImage imageNamed:@"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];
它看起来很有希望,而且很可能是,但不幸的是,它给了我以下信息:

看起来它要么没有被发送到后面,而标题以某种方式显示出来,要么单元格也随之返回。我一点也不确定。这里的任何建议都将不胜感激

这里是我找到编辑代码的地方:

我还没有弄清楚为什么上面的内容不起作用,或者如何修改它使其起作用,但是通过将子视图添加到应用程序委托中的窗口,而不是此处的表视图,我能够使其起作用


我不想只是让它发挥作用,继续前进,而是想真正地学习,所以我仍然很好奇我在上面哪里出错了。因此,我会暂时不回答这个问题,如果你能告诉我我的错误,我会给你投赞成票和绿色复选标记。谢谢

我走过了一条与你在这里描述的相似的道路。我终于在以下帖子中找到了一个适合我的答案:

这是用户rkb发布的两行代码。代码如下:

self.tableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

根据之前的回答(特别是dana_a),我就是这样做的

self.tableView.backgroundColor=[UIColor clearColor]; self.parentViewController.view.backgroundcolor=[uicolorWithPatternImage:[UIImge imageNamed:@“bgtest2.png”]


这是在我的viewwillappease:method中,在我的例子中是一个带有分组tableview的细节视图控制器。

我将这段代码放在我的ViewDidLoad方法中,它工作得很好

self.termsTableView.backgroundColor = [UIColor clearColor];

self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"metal4.png"]];

如果这里没有其他解决方案,那么还有另一个解决方案。在ViewDidLoad中:

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.tableView.window addSubview:bgView];

    [self.tableView addSubview:bgView];
    [self.tableView sendSubviewToBack:bgView];
UIImage *background = [UIImage imageNamed:@"MyBackground.png"];
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:background];
这会将静态(不拉伸或移动)背景放置在所有分组的tableview元素后面,例如标题和单元格