iPhone - UITableView background image loads only on every SECOND build
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.
Do you see the same if you move this code to viewWillAppear
instead?
FWIW it would be better to set your backgroundView.frame
to self.table.bounds
rather than hard coding it to the size of the screen.
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.
精彩评论