[英]iPhone UITableView PlainStyle with custom background image - done “entirely” in code
I have been all over the place, seems the UITableView
with a static background issue is well documented, but no one with a straight forward solution? 我到处都是,看来UITableView
带有静态背景问题已经有很好的记录,但是没有一个人有直接的解决方案吗? Im building my TableViews
entirely in code, like this: 我完全用代码构建我的TableViews
,如下所示:
UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];
The window is my main UIWindow
build for me in the app delegate. 该窗口是我在app委托中为我构建的主要UIWindow
。 From here on I need to build a few different TableViews
(controlled by the navigationController
), some with fetchedResultsControllers
, custom cells and so on. 从这里开始,我需要构建一些不同的TableViews
(由navigationController
控制),一些使用fetchedResultsControllers
,自定义单元格等等。 I prefer to do this completely in code, not using nib's as this would result in either having customization spread between code and IB or having to build and maintain 6+ different Nibs. 我更喜欢在代码中完全执行此操作,而不是使用nib,因为这会导致在代码和IB之间传播自定义,或者必须构建和维护6个以上的不同Nib。
I simply can't find a working example where a tableViewController
Class sets it's own background image. 我根本找不到一个工作示例,其中tableViewController
类设置它自己的背景图像。 If I do this inside one of my TableViews
(extending UITableViewController
): 如果我在我的一个TableViews
(扩展UITableViewController
)中执行此操作:
self.tableView.backgroundColor = backgroundColor;
I, of course, get the tableView's background colored (which incidentally colors the cell's as well, think the cell's inherits their color from the tableView
?) but I wish to have a static background image that my cells slide up and down on top of. 当然,我得到了tableView的背景颜色(它也偶然为单元格着色,认为单元格是从tableView
继承它们的颜色吗?)但我希望有一个静态背景图像,我的单元格在其上下滑动。 Not a "background image" that slides up and down with the users gestures. 不是用户手势上下滑动的“背景图像”。 Exactly what the GroupedStyle tableView offers, but in a PlainStyle tableView:) .. and done using code, not IB. 正是GroupedStyle tableView提供的,但在PlainStyle tableView :) ..并使用代码完成,而不是IB。
I guess I have to clear the background color of the table view, then set the Cells color when configuring them so they don't turn out transparent. 我想我必须清除表格视图的背景颜色,然后在配置它们时设置单元格颜色,这样它们就不会透明。 And then somehow "sneak" a background image below the tableView view from inside the tableView instance? 然后以某种方式“偷偷”从tableView实例里面的tableView视图下面隐藏一个背景图片?
How will I go about this, the best solution would to be able to do this in viewDidLoad or any other function inside my TableViewController, to keep all my customization in one place. 我将如何解决这个问题,最好的解决方案是能够在viewDidLoad或我的TableViewController中的任何其他函数中执行此操作,以便将我的所有自定义保存在一个位置。
Hope someone can help me, Im all 'googled out' :) Thanks! 希望有人可以帮助我,我所有'谷歌搜索':)谢谢!
You need to set up your controller as a UIViewController
, not a UITableViewController
. 您需要将控制器设置为UIViewController
,而不是UITableViewController
。 Then add the tableview
programmatically above a background imageView
. 然后添加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
All of this jumping through hoops is unnecessary if you're targeting > iOS 3.2, where you can use the new backgroundView property to set a UIImageView directly, eg: 如果你的目标是> iOS 3.2,你可以使用新的backgroundView属性直接设置UIImageView,例如:
// In your UITableViewController subclass
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
self.tableView.backgroundView = view;
}
Ok, now it is running:) My tableView was not populated with my cells, so breakPointing through the thing I found out that even though I had implemented the TableViewDataSource and TableViewDelegate, this was only in the main view, I needed to set the delegate and datasource of the tableview to = self. 好吧,现在它正在运行:)我的tableView没有填充我的单元格,所以breakPointing通过我发现的东西,即使我已经实现了TableViewDataSource和TableViewDelegate,这只是在主视图中,我需要设置委托和tableview的数据源为= self。 For others seeking an answer to this here is the method as it ended up with Coneybeares help: 对于其他寻求答案的人来说,这是方法,因为它最终得到了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;
} }
Thanks Coneybeare. 谢谢Coneybeare。
It doesn't crash anymore and the background image turns up just perfect (along with my navigationController in the top) However, still no visible tableView? 它不再崩溃,背景图像变得非常完美(与我的navigationController一起在顶部)然而,仍然没有可见的tableView? just the background image and the navigationControllerBar: 只是背景图片和navigationControllerBar:
This is my implementation: 这是我的实施:
- (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];
} }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
return 1;
} }
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
return 3;
} }
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Hello, World";
return cell;
} }
//commit edit, didSelectRow, memory … etc. //提交编辑,didSelectRow,记忆......等
The forum wasn't up for an entire .m file in one go. 该论坛一次性完成整个.m文件。 Please tell me if I left something out that could help indicate an error. 如果我遗漏了一些有助于表明错误的内容,请告诉我。
I thought maybe it was the order of the layers and tried this without any luck: 我想也许这是层的顺序,并尝试这没有任何运气:
[self.view sendSubviewToBack:imageView];
Hope I missed something obvious. 希望我错过了一些明显的事
Thanks for your time:) 谢谢你的时间:)
Some tips: 一些技巧:
If using a UISplitViewController
: 如果使用UISplitViewController
:
splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
If using a UINavigationController
: 如果使用UINavigationController
:
navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
If using both a UISplitViewController
and a UINavigationController
: 如果同时使用UISplitViewController
和UINavigationController
:
navigationController.view.backgroundColor = [UIColor clearColor]; splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
Be sure to set the background color of any UIView
you want to see through that is on top of the UINavigationController
or UISplitViewController
to [UIColor clearColor]
. 一定要将UINavigationController
或UISplitViewController
顶部的任何UIView
的背景颜色设置为[UIColor clearColor]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.