【发布时间】:2014-02-19 08:23:11
【问题描述】:
我正在尝试使用addChildViewController 功能在其他UIViewcontroller 之上显示UIViewController。childViewController 是一个tableView,它显示在我的MainViewController 之上但是我看不到它所拥有的表格视图。如果我单独执行 childViewController,tableView 工作正常,所以我在这里缺少什么。
这是我添加 childVC 的方式:
@implementation Test2ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)showChildVC:(id)sender
{
TestTableViewController *tVC = [[TestTableViewController alloc]init];
tVC.view.frame = CGRectMake(50, 50, 200, 200);
[self addChildViewController:tVC];
[self.view addSubview:tVC.view];
[tVC didMoveToParentViewController:self];
}
这是我要展示的 childVC:.h
#import <UIKit/UIKit.h>
@interface TestTableViewController : UIViewController<UITableViewDataSource>
{
NSArray *array;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
还有:.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
array = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (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];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
【问题讨论】:
-
UITableView的代理和数据源你设置了吗?
-
是的......如果我单独执行表格视图,它可以正常工作......
-
你的
showChildVC:方法肯定被调用了? -
试过
initWithNibName:初始化TestTableViewController? -
@James Frost: 是的 showChildVC: 被调用....视图显示为灰色背景,但表格视图不显示。
标签: ios objective-c uitableview uiviewcontroller uicontainerview