Why does my UITableView change from UITableViewStyleGrouped to UITableViewStylePlain
My application has a view controller that extends UITableViewController
. The initialization method looks like this:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithCoder:coder]) {
self.tableView = [[UITableView alloc] initWithFrame:self.tableView.frame
style:UITableViewStyleGrouped];
}
return self;
}
When the view is initially loaded, it's displayed as UITableViewStyleGrouped
. However, if my app ever receives a low memory warning, the above view changes to UITableViewStylePlain
. There is no associated xib file with the View/Controller. The viewDidUnload and didReceiveMemoryWarning methods are straightforward:
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any 开发者_C百科retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
My question is, why does the table style change when I receive a memory warning?
It happens on the device and on the simulator. The device is a 3G iphone running OS 3.1.3, the simulator is running OS 3.1
In your initialization, you call [super initWithCoder:coder]
. It would probably be better to override the designated initializer for UITableViewController
, which is -initWithStyle:
. What's probably happening is that when you create the table view controller by calling [super init…]
, it's being created with its tableView
property already being set; that is, it's creating the table view on initialization. That's why your call to self.tableView.frame
works—that shouldn't work if the value of self.tableView
is nil
. Here's a better implementation:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}
精彩评论