我正在尝试实现以下方案: 有一个名为“学习的屏幕 (此屏幕是我的标签栏控制器的rootViewController)
在此屏幕中,我需要显示两个不同的表格:字词和字词
我需要以编程方式执行此操作(即,没有nib文件)。所以我正在为研究视图控制器编写加载视图:
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
//creating the view for the screen "Study"
CGRect cgRct = CGRectMake(0, 0, 320, 367); //define size and position of view
UIView *myStudyView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
myStudyView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
myStudyView.backgroundColor = [UIColor brownColor];
self.view = myStudyView; //set view property of controller to the newly created view
[myStudyView release];
//creating the view for the "words" and instantiating the view controller
WordsTableViewController *wordsTVC = [[[WordsTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
cgRct = CGRectMake(0, 10, 320, 100);//define size and position of view
UIView *myWordsView = [[UIView alloc] initWithFrame:cgRct];//initilize the view
myWordsView.autoresizesSubviews = YES;//allow it to tweak size of elements in view
wordsTVC.view = myWordsView;//set view property of controller to the newly created view
[myWordsView release];
[self.view addSubview:wordsTVC.view];
//creating the view for the "phrases" and instantiating the view controller
PhrasesTableViewController *phrasesTVC = [[[PhrasesTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
cgRct = CGRectMake(0, 120, 320, 100); //define size and position of view
UIView *myPhrasesView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
myPhrasesView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
phrasesTVC.view = myPhrasesView; //set view property of controller to the newly created view
[myPhrasesView release];
[self.view addSubview:phrasesTVC.view];
}
我只看到研究屏幕的棕色背景颜色。这两个表没有出现。我肯定会犯一些根本性的错误,因为它是iPhone开发的全新手。 BTW,WordsTableViewController和PhrasesTableViewController分别定义了TableViewController子类。
感谢您的帮助。 -Achilles
答案 0 :(得分:2)
你为什么这样做?
UIView *myWordsView = [[UIView alloc] initWithFrame:cgRct];
myWordsView.autoresizesSubviews = YES;
wordsTVC.view = myWordsView;
你已经有效地告诉视图控制器不要使用它对UITableView的内部引用,而是使用一些空的UIView作为它的视图。
删除这些行并使用您在视图控制器视图中指定的CGRect调用setFrame。你应该看到你的表视图。像这样:
- (void)loadView {
//creating the view for the screen "Study"
CGRect cgRct = CGRectMake(0, 0, 320, 367); //define size and position of view
UIView *myStudyView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
myStudyView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
myStudyView.backgroundColor = [UIColor brownColor];
self.view = myStudyView; //set view property of controller to the newly created view
[myStudyView release];
//creating the view for the "words" and instantiating the view controller
WordsTableViewController *wordsTVC = [[[WordsTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
cgRct = CGRectMake(0, 10, 320, 100);//define size and position of view
[[wordsTVC view] setFrame:cgRct];
[self.view addSubview:wordsTVC.view];
//creating the view for the "phrases" and instantiating the view controller
PhrasesTableViewController *phrasesTVC = [[[PhrasesTableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
cgRct = CGRectMake(0, 120, 320, 100); //define size and position of view
[[phrasesTVC view] setFrame:cgRct];
[self.view addSubview:phrasesTVC.view];
}