Stumped on nil tableview.

Hey everyone.

Currently I have the following setup in Interface Builder:

UITableViewController ---> UIView ---> TableView

Now my reloadData function works when called using a button, however, viewWillAppear and viewDidAppear won't reload the data. Also, under viewWillAppear and viewDidAppear, self.tableView shows up as null. When I call the reloadData from a button it shows that self.tableView has a value. Any clues as to why this might be happening?

MacBook Pro, Mac OS X (10.6.4)

Posted on Nov 3, 2010 7:09 PM

Reply
5 replies

Nov 4, 2010 2:08 PM in response to Coolio098

Hi Coolio, and welcome to the Dev Forum!
Coolio098 wrote:
... under viewWillAppear and viewDidAppear, self.tableView shows up as null. When I call the reloadData from a button it shows that self.tableView has a value.

I tried various nib configurations, but was unable to reproduce the behavior you're seeing. I seem to remember seeing it in the past, though--just can't remember how I did it. I thought the behavior might have been related to setting the 'view' property of the controller to the UIView object which was the superview of the table view (as in your diagram), but when I tried that today, all I got was: 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "MyController" nib but didn't get a UITableView.'
Any clues as to why this might be happening?

It might be related to the extra view in your nib. I'm also wondering if you implemented loadView (instead of, or in addition to viewDidLoad). That's the best I can do without reproducing the behavior. Btw, do you need that extra view? E.g. does it have other subviews besides the table view? If not, I would recommend taking it out of the nib. whether that fixes the current problem or not.

Anyway, communicating with your table view from viewDidAppear: should be easy, if you'll settle for a workaround instead of an explanation. If the controller's 'view' outlet is connected to the table view in the nib (based on the exception I'm seeing, there's no other choice for a subclass of UITableView), just send your messages to that address. E.g.:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%s: view=%@ tableView=%@", _func_, self.view, self.tableView);
[(UITableViewController*)self.view reloadData];
}

Else, if you somehow managed to connect the 'view' outlet to the table view's superview without getting an exception, try something like this:

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%s: view=%@ tableView=%@", _func_, self.view, self.tableView);
// try afterDelay:.001 etc., if 0.0 doesn't work
[self performSelector:@selector(reloadTableViewData) withObject:nil afterDelay:0.0];
}
- (void)reloadTableViewData {
NSLog(@"%s: view=%@ tableView=%@", _func_, self.view, self.tableView);
[self.tableView reloadData];
}

If none of the above is helpful, please post the class files for your controller, along with more details about the controller's nib (e.g. the exact connections). It might also help to know which template you started with, and if you touched MainWindow.nib. Lastly, please include the Console log messages produced by running one of the above code examples.

- Ray

Nov 9, 2010 3:13 PM in response to Coolio098

Hello,

I was facing the same problem before and what i did is below :

In Interface File i add


@interface MyClass : UIViewController <UITableViewDelegate , UITableViewDataSource>{
IBOutlet UITableView *myTableView;
}


after that i linked this IBOutlet To UITableVIew Object which is on my (VIEW)

and on viewDidAppear i simply write this :


- (void) viewDidAppear:(BOOL)animated{

[myTableView reloadData];
[super viewDidAppear:animated];
}


and by doing this way i solved my problem.

Nov 25, 2010 10:50 PM in response to RayNewbie

Hey everyone!

Sorry for the late response 😝. The reason I have the UIView is because it was the easiest way to set a background without getting the disfiguration for the tableView cells. If there is another way I could set a background without the UIView that would be great because if I remove that view everything works perfectly.

Thank you for the responses they're really appreciated!

Nov 26, 2010 8:50 AM in response to Coolio098

WOW I finally figured it out!

When you have a structure set up like this:

UITableViewController ---> UIView ---> UITableView

the viewWillAppear & viewDidAppear methods run for the UIView before the UITableView. Therefore, the UITableView was showing up as nil when I ran


[self.tableView reloadData];


Basically what I did was pull up the subviews of the UIView which then showed that there was a UITableView beneath it. Therefore, I used the following line to reload the data:


[[self.view.subviews objectAtIndex:0] reloadData];


Message was edited by: Coolio098

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Stumped on nil tableview.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.