views:

378

answers:

1

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

+1  A: 

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;
}
Jeff Kelley
I think you're exactly right. It makes sense anyway. But now when I select a cell, the didSelectCellAtIndexPath method is called, a new view controller is created, and I call [self.navigationController pushViewController:newController animated:YES]; But the new controller is not shown. I can't see how that change would cause this. I feel like I'm missing some basic concept here.
casper
What is the output of `self.navigationcontroller`? Do you have a navigation controller? If not, create one with this table view controller as its root view controller.
Jeff Kelley
self.navigation controller was in fact nil. At that point, I stepped back from the whole thing. I didn't write the original code and as far as I can tell, there's no reason to use initWithCoder: as the init method at all. So I commented out all the init methods, made an xib file and set the table style to grouped in that. Then I linked them to the UITabBar in IB, and now everything works. Still, your answer to my original question was correct, so thank you!
casper