[英]UITableViewController delegate
我有一个UITableViewController类,称为“ MasterViewController”。 从MasterViewController内部,我想显示一个使用其他UITableViewController(而不是MasterViewController)的表视图。 我这样做是因为另一个表视图已经在使用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方法没有被调用。 实际上,没有调用任何委托方法。
对于任何反馈,我们都表示感谢。
提姆
我很确定您的问题是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.