UITableViewController委托

时间:2013-04-01 23:52:05

标签: ios objective-c xcode

我有一个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 cellForRowAtIndexPath方法没有被调用。实际上,没有调用任何委托方法。

任何反馈都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我很确定您的问题是toController过早发布。使用ARC(我假设你也是),我按照你正在尝试的东西来玩它。我得到了相同的结果,其中委托方法似乎没有被调用。解决它的原因是不为toController使用局部变量。而是将其声明为MasterViewController类的成员,如

@property (strong, nonatomic) ToTableViewController *toController; 

然后使用变量名_toController在代码中引用它。

编辑:为了清楚我的第一个测试,我有一个从UITableViewController继承的ToTableViewController。因为我真的不需要添加UITableView你也创建和附加委托(你可以直接使用_toController.view)所以在第二次测试中我创建了一个ToTableViewController,从头开始继承委托协议,其中单独的UITableView变得必要。为了完整起见,这里的代码是有效的:

ToTableViewController.h

#import <Foundation/Foundation.h>

@interface ToTableViewController : NSObject <UITableViewDelegate, UITableViewDataSource>

@end

ToTableViewController.m

#import "ToTableViewController.h"

@implementation ToTableViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 13;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSString *str1 = @"Row #";
    cell.textLabel.text = [str1 stringByAppendingFormat:@"%d", indexPath.row+1];

    return cell;
}

@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
相关问题