multiple tables on single screen programmatically (without a xib file) iphone
I am trying to implement the following scenario: There is a screen called: Study (this screen is the rootViewController of my tab bar controller)
In this screen, I need to display two different tables: words and phrases
I need to do this programatically (i.e. without a nib file). So I am writing the loadview for the Study view Controller as:
// 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 开发者_StackOverflow社区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];
}
I just see the brown background color of the study screen. The two tables don't appear. I sure am making some fundamental mistake as am a total newbie to iPhone development. BTW, WordsTableViewController and PhrasesTableViewController are separately defined TableViewController subclasses.
Appreciate your help. -Achilles
Why are you doing this?
UIView *myWordsView = [[UIView alloc] initWithFrame:cgRct];
myWordsView.autoresizesSubviews = YES;
wordsTVC.view = myWordsView;
You have effectively told the view controller not to use it's internal reference to its UITableView but to instead use some empty UIView as its view.
Remove those lines and call setFrame with the CGRects you've specified on the view controller's view. You should see your table views then. Like this:
- (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];
}
精彩评论