Why is my UITableView changing 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 appears as UITableViewStyleGrouped
. However, if my application ever gets a low memory warning, the above view changes to UITableViewStylePlain
. There is no associated xib file with View / Controller. The viewDidUnload and didReceiveMemoryWarning methods are simple:
- (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 get a memory warning?
This happens on the device and on the simulator. The device is a 3G iphone running OS 3.1.3, the simulator runs OS 3.1
a source to share
In your initialization, you call [super initWithCoder:coder]
. It would probably be better to override the designated initializer for UITableViewController
which is -initWithStyle:
. This probably happens when you create a table view controller, by calling [super initβ¦]
it is created with the property already set tableView
; that is, it creates a table view on initialization. This is why your call works self.tableView.frame
, which shouldn't work if the value self.tableView
is equal nil
. Here's a better implementation:
- (id)initWithCoder:(NSCoder*)coder {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
}
return self;
}
a source to share