Ios UITableViewController委托

Ios UITableViewController委托,ios,objective-c,xcode,Ios,Objective C,Xcode,我有一个UITableViewController类,我称之为MasterViewController。 在MasterViewController中,我希望显示使用不同UITableViewController而不是MasterViewController的tableview。我这样做是因为另一个tableview已经使用MasterViewController作为其委托和数据源 在MasterViewController方法中,我有以下逻辑: ToTableViewController *t

我有一个UITableViewController类,我称之为MasterViewController。 在MasterViewController中,我希望显示使用不同UITableViewController而不是MasterViewController的tableview。我这样做是因为另一个tableview已经使用MasterViewController作为其委托和数据源

在MasterViewController方法中,我有以下逻辑:

ToTableViewController *toController = [[ToTableViewController alloc] init];
UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,250,200)];
//toView.backgroundColor = [UIColor redColor];
UITableView *toTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,220,180) style:UITableViewStylePlain];

[toTableView setDelegate:toController];
[toTableView setDataSource:toController];

[toView addSubview:toTableView];

[self.view addSubview:toView];

[toTableView reloadData];
我希望ToTableViewController成为这个新的tableview toTableView的委托和数据源

问题是没有调用ToTableViewController CellForRowatineXpath方法。实际上,没有调用任何委托方法

如有任何反馈,将不胜感激


蒂姆

我很确定你的问题是toController发布得太早了。使用ARC,我想你也是,我按照你想做的事情来玩它。我得到了相同的结果,委托方法似乎没有被调用。解决这个问题的方法是不为toController使用局部变量。而是将其声明为MasterViewController类的成员,如

@property (strong, nonatomic) ToTableViewController *toController; 
然后使用变量名_toController在代码中引用它

编辑:在我的第一次测试中,我让ToTableViewController继承自UITableViewController。因此,我真的不需要您正在创建和附加委托的添加的UITableView,您可以直接使用_toController.view,因此在第二次测试中,我从头创建了一个ToTableViewController,它继承了委托协议,其中需要单独的UITableView。为了完整起见,下面是有效的代码:

ToTableViewController.h

#import <Foundation/Foundation.h>

@interface ToTableViewController : NSObject <UITableViewDelegate, UITableViewDataSource>

@end
MasterViewController.m我在.m文件中声明toController,如图所示,但可以在.h文件中声明

#import "MasterViewController.h"
#import "ToTableViewController.h"

@interface MasterViewController ()

@property (strong, nonatomic) ToTableViewController *toController;

@end

@implementation MasterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _toController = [[ToTableViewController alloc] init];
    UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,250,200)];
    toView.backgroundColor = [UIColor redColor];

    UITableView *toTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,220,180) style:UITableViewStylePlain];

    [toTableView setDelegate:_toController];
    [toTableView setDataSource:_toController];

    [toView addSubview:toTableView];

    [self.view addSubview:toView];
}

@end

jross,谢谢你花时间帮助我。这似乎奏效了。有趣的是,我最初不得不将toController声明为MasterViewController的属性,并且遇到了相同的问题。我按照你的建议把它移回去了,它正在工作。也许在我上一次的xCode会话中有什么东西被破坏了。无论如何,这解决了我的问题。非常感谢。Tim@TDavey,因为我一直在学习iOS,所以我对你的问题很感兴趣,因为我一直在试验w/类似类型的场景。
#import "MasterViewController.h"
#import "ToTableViewController.h"

@interface MasterViewController ()

@property (strong, nonatomic) ToTableViewController *toController;

@end

@implementation MasterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _toController = [[ToTableViewController alloc] init];
    UIView *toView = [[UIView alloc] initWithFrame:CGRectMake(10,10,250,200)];
    toView.backgroundColor = [UIColor redColor];

    UITableView *toTableView = [[UITableView alloc] initWithFrame:CGRectMake(10,10,220,180) style:UITableViewStylePlain];

    [toTableView setDelegate:_toController];
    [toTableView setDataSource:_toController];

    [toView addSubview:toTableView];

    [self.view addSubview:toView];
}

@end