具有自定义背景图像的 iPhone UITableView PlainStyle - “完全”在代码中完成
-
08-07-2019 - |
题
我已经走遍了所有的地方,似乎 UITableView
静态背景问题已有详细记录,但没有人有直接的解决方案?我正在建设我的 TableViews
完全在代码中,如下所示:
UIViewController *tableViewController = [[TableViewController alloc] init];
navigationController = [[UINavigationController alloc]
initWithRootViewController:tableViewController];
[tableViewController release];
[window addSubview:navigationController.view];
窗户是我的主要 UIWindow
在应用程序委托中为我构建。从这里开始我需要构建一些不同的 TableViews
(由控制 navigationController
),有些与 fetchedResultsControllers
, 、自定义单元格等。我更喜欢完全在代码中完成此操作,而不是使用 nib,因为这会导致在代码和 IB 之间进行定制,或者必须构建和维护 6 个以上不同的 Nib。
我根本找不到一个可行的例子 tableViewController
类设置它自己的背景图像。如果我在我的一个里面这样做 TableViews
(延伸 UITableViewController
):
self.tableView.backgroundColor = backgroundColor;
当然,我得到了 tableView 的背景颜色(顺便说一句,它也为单元格着色,认为单元格继承了它们的颜色) tableView
?)但我希望有一个静态背景图像,我的单元格可以在其上上下滑动。不是随着用户手势上下滑动的“背景图像”。正是 GroupedStyle tableView 提供的功能,但是在 PlainStyle tableView 中:) ..并使用代码完成,而不是 IB。
我想我必须清除表格视图的背景颜色,然后在配置它们时设置单元格颜色,这样它们就不会变成透明的。然后以某种方式从 tableView 实例内部“潜入”tableView 视图下方的背景图像?
我将如何解决这个问题,最好的解决方案是能够在 viewDidLoad 或 TableViewController 内的任何其他函数中执行此操作,以将所有自定义保留在一个位置。
希望有人可以帮助我,我都用“谷歌搜索”了:) 谢谢!
解决方案
您需要将控制器设置为 UIViewController
,而不是 UITableViewController
。然后以编程方式在背景 imageView
上添加 tableview
。
@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
其他提示
如果你的目标是&gt;那么所有这些都是不必要的。 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;
}
好的,现在正在运行:) 我的tableView没有填充我的单元格,所以breakPointing通过我发现的东西 即使我已经实现了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;
}
谢谢Coneybeare。
它不再崩溃,背景图像变得非常完美(与我的导航控制器一起在顶部) 但是,仍然没有可见的tableView?只是背景图片和navigationControllerBar:
这是我的实施:
- (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;
}
//提交编辑,didSelectRow,记忆......等等。
论坛一次性完成整个.m文件。 请告诉我是否遗漏了可能有助于表明错误的内容。
我想也许这是图层的顺序,并试着没有任何运气:
[self.view sendSubviewToBack:imageView];
希望我错过了一些明显的东西。
感谢您的时间:)
一些技巧:
如果使用
UISplitViewController
:splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
如果使用
UINavigationController
:navigationController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
如果同时使用
UISplitViewController
和一个UINavigationController
:navigationController.view.backgroundColor = [UIColor clearColor]; splitViewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
确保设置任何背景颜色 UIView
你想看透那是在上面 UINavigationController
或者 UISplitViewController
到 [UIColor clearColor]
.