In my iPhone app I have a UIViewController that has a UITableView and another UIView, in its xib file, the view of the xib contains both the UITableView and the other UIView. Both view are linked as IBOutlets to the appropriate views in the xib file. When I want to set the data source programmatically for the UITableView I create a method like this in the UIViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(id)dataSource{
if ((self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
self.inventoryTableView.dataSource = dataSource;
//[self.inventoryTableView reloadData];
}
return self;}
and then I implement the following methods in another class of mine, let's call it B:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Finally I alloc+init B and pass the instance to the method initWithNibName:bundle:tableDataSource:
creating the UIViewController. All the views appear in the simulator but the data source methods appear not to be called.
I do something similar with another controller and works, but it is a UITableViewController and the the TableView is the controller's view. Is it possible that being a subview i have to set the data source in another method different from the initWithNib?
UPDATE:
By putting the datasource assignation in the viewDidLoad:
method I was able to solve it. It appears to me that even after calling [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]
the subViews of the main view are not properly inited