TableView wont load inside of UINavigationController
I have a problem with loading data to UITableView.
My implementation is following:
@interface FirstViewController : UINavigationController <UIActionSheetDelegate, UITableViewDelegate> {
UITableViewController *tableViewController;
UITableView *table;
}
@end
And viewDidLoad
method is:
- (void)viewDidLoad {
[super viewDidLoad];
tableViewController = [[UITableViewController alloc] init];
table = [[UITableView alloc] init];
tableViewController.tableView = table;
[tableViewController.tableView setDelegate:self];
self.newsList = [NSMutableArray array];
tableViewController.tableView.rowHeight = 130.0;
[self addObserver:self forKeyPath:@"newsList" options:0 context:NULL];
[self pushViewController:tableViewController animated:NO];
}
And I'm can't understand what's wrong. May be data won't load because i'm adding observer ?
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
开发者_如何学运维 context:(void *)context
{
[tableViewController.tableView reloadData];
}
Thanks for any advise !
So if I understand correctly you have a tab bar controller which includes the navigation controller as one of the tabs, the navigation controller creates a table view controller in its viewDidLoad method, and pushes it immediately.
viewDidLoad is a good time to do the loading (which you did) and a bad time to do behaviour, like pushing the view.
You could try moving the pushViewController part to -viewDidAppear:animated:
. this should give you the table view, but it will still behave a little funny cause you'll have a back button that causes the table to disappear then immediately be pushed again.
The best way to go about this is to set the table view controller as the root view controller of the navigation controller.
UPDATE:
Seems like there's a blur of what controller should do what here.
Tab Bar Controller:
don't subclass it, put the navigation view controller as one of the tabs. You got this part right already I think.
Navigation Controller
Don't subclass it, initialize it with your table view controller as the root view controller. Unless you are trying to make the navigation controller do something completely different from what it usually does, there is rarely any need to subclass it.
Table View Controller
Subclass this. It does 90% of the overhead work for you off the top, all you have to do is write implementations for the data source and delegate methods.
This might seem funny at first cause you'll need to initialize them in reverse order. Your app delegate should init the table view controller, then initialize the navigation controller with the table view controller as root, then initialize the tab bar controller with navigation controller as one of the tabs. This is a tricky stack of controllers to get right at first.
You might've forgotten one little thing, dataSource
.
[tableViewController.tableView setDataSource:self];
Add this after the setDelegate:
method.
精彩评论