未调用UITableView委托方法

use*_*234 0 cocoa-touch uitableview ios

我在UIViewController子类中创建了一个UITableViewController并设置了委托方法,但是没有调用数据源委托方法(通过观察日志).我是否需要继承teh UITableViewController?我错过了什么?

在MyViewController.h中

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableViewController *myTableViewController;
}
@property (nonatomic, assign) UITableViewController *myTableViewController;
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)

我从另一个类调用了表视图:

UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:myTableViewController];
Run Code Online (Sandbox Code Playgroud)

Tom*_*mmy 5

A UITableViewController本身就是一个视图控制器.所以你通常不会在视图控制器中创建一个viewDidLoad.您通常要么创建一个UITableView子类UITableViewController,要么让它担心创建和设置相关视图.

即使您正在创建视图控制器,然后劫持其视图以作为委托连接到您,您也无法显示该视图.由于视图永远不会显示,因此它永远不需要与其代理进行通信.您可能打算在自己的内部添加相关视图viewDidLoad.

最后,你实现的两种方法是其中的一部分UITableViewDataSource,而不是UITableViewDelegate.数据源提供表格内容,代表获得有关点击和其他相关事件的信息.所以你可能想把自己设置为数据源,而不是委托.