I created a UITableViewController in a UIViewController subclass and setup delegate methods but the data source delegate methods are not being called (by observing the logs). Do I need to subclass teh UITableViewController? What did I miss?
In MyViewController.h
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableViewController *myTableViewController;
}
@property (nonatomic, assign) UITableViewController *myTableViewController;
In MyViewController.m
- (void)viewDidLoad
{
myTableViewController = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
myTableViewController.tableView.delegate = self;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection");
return [self.assets count]; //assets is NSMutableArray
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [assets objectAtIndex:row]; //assets is NSMutableArray
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}
I called the table view from another class:
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:myTableViewController];
A
UITableViewControlleris itself a view controller. So you wouldn’t normally create one within a view controller’sviewDidLoad. You’d normally either create aUITableViewor subclassUITableViewControllerand let it worry about creating and setting up the relevant view.Even given that you’re creating a view controller, then hijacking its view to connect to you as a delegate, you’re failing to display that view. Since the view is never displayed, it never has any need to talk to its delegate. You probably intended to add the relevant view to your own within
viewDidLoad.Finally, the two methods you’ve implemented are part of
UITableViewDataSource, notUITableViewDelegate. The data source provides table contents, the delegate gets informed about taps and other related events. So you probably want to set yourself up as the data source, not the delegate.