2

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

casper
  • 321
  • 5
  • 10
  • Does this happen on a device, a simulator, or both? What is the base SDK for your project? What SDK are you compiling for? What version of iPhone OS is the app being run on? – Shaggy Frog Apr 14 '10 at 16:57

1 Answers1

4

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
  • 19,021
  • 6
  • 70
  • 80
  • 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 Apr 14 '10 at 19:11
  • 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 Apr 14 '10 at 20:12
  • 1
    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 Apr 16 '10 at 17:43
  • @casper, @Jeff: A few weeks after following this answer, I ran into the navigationController == nil problem you had. It happens because the `[super initWithCoder:coder]` is the bit that actually loads the object from the nib, restoring the original connection to the navigationController. (Maybe you had figured that out by now :) took me some time, though.) – Jesse Millikan Nov 02 '11 at 18:40