简体   繁体   中英

UITableView delegate methods not being called

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 UITableViewController is itself a view controller. So you wouldn't normally create one within a view controller's viewDidLoad . You'd normally either create a UITableView or subclass UITableViewController and 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 , not UITableViewDelegate . 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.

Two things missing from my original code: 1. In MyViewController.h create an table view variable. 2. In drag a table view object in IB to main window (not any view) and make the delegate, data source and view to file's owner. (note the view of the table view object will need to connect to the table view variable you created in step 1.) 3. Add this line to viewDidLoad method:myTableViewController.tableView = tView; //tView is the tableview that you created in step1.

Or you can also subclass the UITableViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM