Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Derek Saunders
5,167 PointsHow to store more than one array in a table view controller.
I have been playing around with table view controllers and just been trying to understand them. I have table view with 2 static cells labeled make and model for cars. When you select a make it pops the table view controller and stores it in a detailTextLabel. Instead of making a table view controller for each individual make to choose your model is it possible to store a multitude of arrays in one table view controller and display it according to what make you have chosen?
Thanks for the help in advance.
6 Answers
Stone Preston
42,016 Pointsif you were to use prepare for segue, you would add an NSArray property to your destinationView controllers class (lets call the property "models". you would also need to store the selected make in a property whenever the user selects a make in the didSelectCellAtIndexPath. then inside the prepareForSegue method you would just assign your models property a value
this code is inside the didSelectCellAtIndexpath method of the makeView controller. you would need to add the selectedMake property to your class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//set the selectedMake property to the text of the make label
self.selectedMake = cell.makeLabel.text
...
}
this code is inside prepare for segue of your make view controller. you conditionally assign the array of models depending on the selected make
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"showModels"]) {
ModelsViewController *viewController = (ModelsViewController *)segue.destinationViewController;
//set the model property of your ModelViewController depending on the selected make
if ([self.selectedMake isEqualToString:@"acura"]) {
viewController.models = @[@"abc123", @"abc456", @"abc789"];
} else if ([self.selectedMake isEqualToString:@"jaguar"]) {
viewController.models = @[@"jjj123", @"jjj456", @"jjj789"];
}
}
}
and so on and so on. then just set up your modelsViewControllers tableView to use the models property for its data. then it will display the models you conditionally set in the previous make view.
thats a very simple way to do it. are you using a database or anything or is this just sort of a learning experience kind of thing. with a lot of makes and models it could get tedious.
Stone Preston
42,016 Pointshmm can you clarify a bit. when you say it pops the tableView controller and stores it in a detail text label what do you mean by that. do you mean it pops the table view from the view stack and stores the selected cell data in text label? maybe to post some screen shots so I can get a better idea of how your apps designed. what you want to do is definitely possible and its probably easiest to just use an if statement inside the prepare for segue method.
Derek Saunders
5,167 PointsStone Preston I was implementing another models array, but in the destination table view controller what would I put in the
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.abarthModels count];
return [self.acuraModels count];
}
When I put this it doesn't display anything.
Stone Preston
42,016 Pointsin your models view controller you should only be using one array property. you conditionally set this array in the previous view controller. so lets say you called that property modelsArray. in prepare for segue of the makeView controller you have:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"showModels"]) {
ModelsViewController *viewController = (ModelsViewController *)segue.destinationViewController;
//set the model property of your ModelViewController depending on the selected make
if ([self.selectedMake isEqualToString:@"acura"]) {
viewController.modelsArray = @[@"abc123", @"abc456", @"abc789"];
} else if ([self.selectedMake isEqualToString:@"jaguar"]) {
viewController.modelsArray = @[@"jjj123", @"jjj456", @"jjj789"];
}
}
}
then in your tableViewController delegate methods in your models controller you just reference that modelsArray property. you dont want multiple arrays, just one.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.modelsArray count];
}
Derek Saunders
5,167 Pointsyes exactly I use a delegate to store the selected item in the detailTextLabel. http://stackoverflow.com/questions/24826327/creating-a-list-after-clicking-on-a-cell
this link in stack overflow gives a picture of what I'm trying to do. Now if I were to use prepare for segue, how would I declare the appropriate array in the destination controller? Stone Preston
Derek Saunders
5,167 PointsMore for a learning experience to really understand how they work, I really appreciate your help Stone Preston This is more than I could have asked for. Thanks so much!
Stone Preston
42,016 Pointsno problem. I think the code I posted should work pretty easily. I didnt test it or anything but it should work. let me know if you run into any issues or cant figure something out.
Stone Preston
42,016 Pointsalso you may notice I made some assumptions about your code. I assumed that you would call the segues certain things and name your view controllers certain things so you may need to make some changes depending on how you have stuff named etc.
Derek Saunders
5,167 Points@stonepreston thank you I did notice that and it is working like a charm! Thanks!
Derek Saunders
5,167 PointsStone Preston okay I was trying to create different array names for each array, didn't realize I was supposed to keep it under one name haha, thanks a lot.