带自定义背景图像的iPhone UITableView平面样式-完成”;完全地;编码

带自定义背景图像的iPhone UITableView平面样式-完成”;完全地;编码,iphone,uitableview,background-image,Iphone,Uitableview,Background Image,我到处都去过,似乎有静态背景问题的UITableView有很好的文档记录,但是没有人有直接的解决方案? 我完全用代码构建我的表视图,如下所示: UIViewController *tableViewController = [[TableViewController alloc] init]; navigationController = [[UINavigationController alloc] initWithRootViewCon

我到处都去过,似乎有静态背景问题的
UITableView
有很好的文档记录,但是没有人有直接的解决方案? 我完全用代码构建我的
表视图
,如下所示:

    UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
                        initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];
该窗口是我在应用程序代理中为我构建的主
ui窗口。从这里开始,我需要构建一些不同的
表格视图
(由
导航控制器
控制),其中一些带有
获取的ResultsController
、自定义单元格等。我更喜欢完全在代码中完成这项工作,而不是使用nib,因为这将导致在代码和IB之间进行定制,或者必须构建和维护6+个不同的nib

我根本找不到一个工作示例,其中
tableViewController
类设置了自己的背景图像。如果我在一个
表视图中执行此操作
(扩展
UITableViewController
):

一、 当然,给tableView的背景上色(顺便说一句,这也会给单元格上色,认为单元格的颜色继承了
tableView
?)的颜色),但我希望有一个静态的背景图像,我的单元格可以在上面上下滑动。不是一个随着用户手势上下滑动的“背景图像”。 这正是GroupedStyle tableView提供的,但在一个简单的tableView中:)。。并使用代码完成,而不是IB

我想我必须清除表格视图的背景色,然后在配置单元格时设置单元格颜色,这样它们就不会变成透明的。然后从tableView实例内部“潜入”tableView视图下方的背景图像

我将如何着手,最好的解决方案是能够在viewDidLoad或TableViewController中的任何其他函数中执行此操作,以将所有自定义项保留在一个位置


希望有人能帮我,我都被谷歌搜索出来了:)谢谢

您需要将控制器设置为
UIViewController
,而不是
UITableViewController
。然后以编程方式将
tableview
添加到背景
imageView
上方

@interface SomeController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
  ...
  UITableView *tableView;
  ...
}
@property (nonatomic, retain) UITableView *tableView;
@end



@implementation SomeController

@synthesize tableView;

...

- (void)loadView {
    [super loadView];
    UIImageView *v = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
    [v setImage:[UIImage imageNamed:@"table_background.png"]];
    [self.view addSubview:v];


    self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.tableView];
}
...
@end
@接口SomeController:UIViewController{
...
UITableView*表格视图;
...
}
@属性(非原子,保留)UITableView*tableView;
@结束
@控制器的实现
@综合桌面视图;
...
-(void)负荷视图{
[超级加载视图];
UIImageView*v=[[UIImageView alloc]initWithFrame:self.view.bounds]autorelease];
[v setImage:[UIImage ImageName:@“table_background.png”];
[self.view addSubview:v];
self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds]autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}
...
@结束
谢谢康奈比尔

它不再崩溃,背景图像显示得非常完美(顶部还有我的navigationController) 但是,仍然没有可见的tableView?只有背景图像和导航控制器栏:

这是我的实现:

- (void)loadView {
[super loadView];

UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];

[self.tableView = [[UIView alloc] initWithFrame:self.view.bounds] autorelease];

[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];
}

}

}

}

//提交编辑、编辑行、内存…等

论坛并不是一次就可以得到一个完整的.m文件。 请告诉我是否遗漏了有助于指示错误的内容

我想这可能是层的顺序,并尝试了这个没有任何运气:

    [self.view sendSubviewToBack:imageView];
希望我错过了一些明显的东西

谢谢您的时间:)

好的,现在它正在运行:) 我的tableView没有填充我的单元格,因此在我发现的情况下中断 尽管我已经实现了TableViewDataSource和TableViewDelegate,但这只是在主视图中,我需要将tableview的委托和数据源设置为=self。 对于其他寻求答案的人,这里是一个方法,因为它最终得到了Coneybeares的帮助:

- (void)loadView {
[super loadView];

UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.view.bounds] autorelease];
[imageView setImage:[UIImage imageNamed:@"carbon_background.png"]];
[self.view addSubview:imageView];

[self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds] autorelease];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:self.tableView];

self.tableView.delegate = self;
self.tableView.dataSource = self;
}一些提示:

  • 如果使用
    UISplitViewController

    self.tableView.backgroundColor = backgroundColor;
    
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor clearColor];
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
  • 如果使用
    UINavigationController

    self.tableView.backgroundColor = backgroundColor;
    
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor clearColor];
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
  • 如果同时使用
    UISplitViewController
    UINavigationController

    self.tableView.backgroundColor = backgroundColor;
    
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    navigationController.view.backgroundColor = [UIColor clearColor];
    splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    

确保将
UINavigationController
UISplitViewController
上方的任何
UIView
的背景色设置为
[UIColor clearColor]

如果您的目标是iOS 3.2,那么所有这些跳转都是不必要的,您可以使用新属性直接设置UIImageView,例如:

// In your UITableViewController subclass
- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    self.tableView.backgroundView = view;
}  

感谢Coneybeare,这似乎遵循了我在NIB中看到的逻辑,它包含了自定义图像、构建通用视图、将imageview和tableView放在其中。我确实有一些奇怪的行为:/我实现了CellForRowatineXpath:并且它还有一个名为“tableView”的局部变量。这会导致编译器警告“本地声明'tableView'隐藏实例变量”。然后我尝试将变量重命名为tableViewLocal,但警告仍然存在-继续…加载视图时(如我的问题中所述),当我添加[self.tableView addSubview:self.tableView]时,它会崩溃;它不是应该是:[self.view addSubview:self.tableView];这是编译和工作,但只有背景图像,没有表格?我认为这种方法是合理的,但是我缺少了一些东西:)Thans:)我会将方法签名中的所有“tableView”参数更改为tableView,并保持self.tableView不变。崩溃是我的一个输入错误,应该是self.view add…(现已修复)self.tableView=[UIView alloc]应该是self.tableView=[UITableView alloc]。对我来说,这已经足够修复崩溃了。刚刚得到它:)self.tableView=[[UIView alloc]initWithFrame:self.view.bounds]autorelease];需要是:self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds]autorelease];否则它将不“适合”:)再次感谢!