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 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 forUITableViewController, 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 itstableViewproperty already being set; that is, it’s creating the table view on initialization. That’s why your call toself.tableView.frameworks—that shouldn’t work if the value ofself.tableViewisnil. Here’s a better implementation: