In my iPhone application, I have a UIViewController with a UITableView and another UIView, in its xib file, the xib view contains both a UITableView and another UIView. Both views are linked as IBOutlets with the corresponding views in the xib file. When I want to set a data source programmatically for a UITableView, I create such a method in the UIViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(id)dataSource{
if ((self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
self.inventoryTableView.dataSource = dataSource;
}
return self;}
and then I implement the following methods in another class, let myself B:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Finally, I highlight + init B and pass the instance to the method initWithNibName:bundle:tableDataSource:by creating a UIViewController. All views are displayed in the simulator, but the data source methods do not seem to be called. I am doing something similar with another controller and it works, but it is a UITableViewController and TableView is a view of the controller. Is it possible that to be a subview I have to set the data source in a different method than initWithNib?
UPDATE:
By placing the data source assignment in a method viewDidLoad:, I was able to solve it. It seems to me that even after the call [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil], the main view subvisors will not be correctly entered
source
share