简体   繁体   English

UITableViewController委托

[英]UITableViewController delegate

I have a UITableViewController class that I call "MasterViewController". 我有一个UITableViewController类,称为“ MasterViewController”。 From within MasterViewController I want to display a tableview that uses a different UITableViewController (not MasterViewController). 从MasterViewController内部,我想显示一个使用其他UITableViewController(而不是MasterViewController)的表视图。 I am doing this as another tableview is already using MasterViewController as its delegate and datasource. 我这样做是因为另一个表视图已经在使用MasterViewController作为其委托和数据源。

I have the following logic in a method of 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];

I want the ToTableViewController to be the delegate and datasource for this new tableview (toTableView). 我希望ToTableViewController成为此新tableview(toTableView)的委托和数据源。

The problem is that my ToTableViewController cellForRowAtIndexPath method is not being called. 问题是我的ToTableViewController cellForRowAtIndexPath方法没有被调用。 In fact, none of the delegate methods are being called. 实际上,没有调用任何委托方法。

Any feedback would be appreciated. 对于任何反馈,我们都表示感谢。

Tim 提姆

I am pretty sure your issue is that the toController is being released too early. 我很确定您的问题是toController发布得太早了。 Using ARC (I assume you are too), I played around with it following what you are trying todo. 使用ARC(假设您也是),我按照您的尝试进行了操作。 And I got the same result where the delegate methods appeared NOT to get called. 在没有出现委托方法的情况下,我得到了相同的结果。 What solved it was to NOT use a local variable for the toController . 解决问题的是toController使用局部变量。 Instead declare it as a member to the MasterViewController class like 而是将其声明为MasterViewController类的成员,例如

@property (strong, nonatomic) ToTableViewController *toController; 

then use the variable name _toController to refer to it in the code. 然后使用变量名_toController在代码中引用它。

EDIT: just to be clear in my first test I had ToTableViewController inherit from a UITableViewController. 编辑:为了清楚起见,在我的第一个测试中,我从UITableViewController继承了ToTableViewController。 Since as such I really didn't need that added UITableView you are creating and attaching the delegates too (you could just use the _toController.view directly) SO on this second test I created a ToTableViewController from scratch inheriting from the delegate protocols where the separate UITableView becomes necessary. 由于这样,我真的不需要添加UITableView,因此您也可以创建并附加委托(您可以直接使用_toController.view)。因此,在第二个测试中,我从头创建了一个ToTableViewController,该委托继承自委托协议,其中UITableView成为必需的。 Just for completeness here is the code that works: 为了完整起见,下面的代码可以正常工作:

ToTableViewController.h ToTableViewController.h

#import <Foundation/Foundation.h>

@interface ToTableViewController : NSObject <UITableViewDelegate, UITableViewDataSource>

@end

ToTableViewController.m 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 (I declare the toController in the .m file as shown but could do so the .h file instead...) 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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