iPhone - UITableView 背景图像仅在每个第二次构建时加载
这让我彻底疯了。
我正在构建一个 iPhone 应用程序,在我的根视图控制器中我有一个 UITableView。 在 viewDidLoad 中,我为此表设置了背景图像,如下所示:
[self.view setBackgroundColor:[UIColor blackColor]];// this is so that I can see when the table background image does not show up.
UIImageView* backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whiteBackground.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 460)];
[self.table setBackgroundView:backgroundView];
[backgroundView release];
我的 tableCells 是透明的(或本身具有背景图像)。
它每隔一次在我构建应用程序并在模拟器和实际设备上运行它时工作。 它会显示得很好,然后当我再次构建时,它将仅显示黑色背景(但表调用有其背景图像/透明度)。
以前有人经历过这样的事情吗?事实上,它每秒都会被拾取并显示,这让我觉得内存中潜藏着一些东西。
当我在设备/模拟器上运行它,然后正常退出应用程序(双击主页,按住图标并点击关闭),然后再次运行它时,它工作正常。这似乎与我每次构建应用程序时杀死该应用程序有关。请注意,我在另一个视图控制器上使用相同的代码,并且每次都有效,但它不适用于我的根视图控制器。
This is driving me utterly insane.
I'm building an iPhone app and in my root view controller I have a UITableView.
In viewDidLoad I've set a background image for this table like so:
[self.view setBackgroundColor:[UIColor blackColor]];// this is so that I can see when the table background image does not show up.
UIImageView* backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"whiteBackground.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 460)];
[self.table setBackgroundView:backgroundView];
[backgroundView release];
My tableCells are transparent (or have background images themselves).
It works every second time I build the app and run it, both on the simulator and on an actual device.
It will show up fine, then when I build again it will show just the black background (but the table calls have their background images/transparency).
Has anyone ever experienced anything like this before? The fact that it is being picked up and displayed every second build makes me think there is something lurking in memory.
When I run it on a device/simulator and then quit the app normally (double tap home, hold the icon and tap closed) and then run it again it works fine. It seems to be something to do with killing the app when I build it each time. Note that I use the same exact code on another viewcontroller and it works every time, but it doesn't work on this - my root view controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将此代码移至
viewWillAppear
,您会看到相同的结果吗?FWIW 最好将
backgroundView.frame
设置为self.table.bounds
而不是将其硬编码为屏幕大小。Do you see the same if you move this code to
viewWillAppear
instead?FWIW it would be better to set your
backgroundView.frame
toself.table.bounds
rather than hard coding it to the size of the screen.我一直在用头撞墙来解决这个问题,并发现了一些事情(正如我在回复安德鲁·埃布林的回复中提到的)。它不仅仅是背景图像。
我的项目设置如下:
带有 XIB 的 UIViewController。
在 XIB 中拖放一个 tableView。
将委托和数据源链接到 UIViewController。
创建一个名为“table”的 IBOutlet UITableView。
在 IB 中链接起来。
这对我来说一直都很有效。但出现了一些奇怪的情况,我的“表”IBOutlet 在 50% 的时间内被识别。我构建的每秒都无法识别,并且每当我调用它时 reloadData 都不会运行。它第一次加载表(意味着数据源完好无损)并且可以选择单元格(意味着委托很好),但发送到“表”的任何选择器都将被忽略。
这只发生在 RootViewController 上。不适用于我使用这种方式执行操作的任何后续视图控制器。
所以我只是将 RootViewController 的类更改为 UITableViewController,相应地更改了 XIB(将 uiview 对象替换为 uitableview 并将文件的所有者视图属性设置为 UItableview)。基本上,当您指定带有 UITableViewController 子类和用于用户界面的 XIB 的新文件时,我只是恢复到 XCode 为您提供的内容。
这样我使用 self.tableView 来重新加载而不是我自己的 IBOutlet 并且它工作正常。
我不知道是什么原因造成的,但想知道这是否是一个错误。
I've been banging my head against a wall to fix this and have found out a few things (as mentioned in my reply to Andrew Ebling's response). It's more than just the background image.
I had my project set up like so:
A UIViewController with a XIB.
Drop a tableView in the XIB.
Link up the Delegate and Data Source to the UIViewController.
Create a IBOutlet UITableView named 'table'.
Link that up in IB.
That has always worked just fine for me. But there is something weird going on with my 'table' IBOutlet being recognised 50% of the time. Every second time I build it is not recognised and reloadData is not run whenever I call it. It loads the table first time (meaning the datasource is intact) and cells can be selected (meaning the delegate is fine) but any selectors sent to 'table' are ignored.
This only happens on the RootViewController. Not on any subsequent view controllers in which I use this way of doing things.
So I just changed the class of my RootViewController to UITableViewController, changed the XIB accordingly (replace the uiview object with a uitableview and set the file's owner view property to the UItableview). Basically I just reverted back to what XCode gives you when you specify a new file with a UITableViewController subclass and a XIB for user interface.
That way I use self.tableView to reload instead of my own IBOutlet and it works fine.
I have no idea what caused this though and would like to know if it's a bug or not.