我试图UIViewController
在其他UIViewcontroller
使用addChildViewController
功能之上显示一个。childViewController 是一个 tableView,它显示在我的 MainViewController 之上,但是我看不到它具有的 table view。如果我单独执行 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;
}