I have a navigation controller with a table view. In most tutorials I've read they usually have an array of view controllers (or subclasses of) stored locally in the table view controller that the开发者_StackOverflowy use to push onto the navigation stack when a table cell is selected.
In my current project I have a lot of data that is loaded from an XML file. This data is kept central in a singleton class. This is my 'model' if you will.
So in my tableview controller I don't have an array of view controllers (I didn't want to have redundant data stored in my view controller when it's already stored elsewhere). Instead I access the data from the singleton and create a view controller, initialize it with the appropriate data and then push that onto the navigation stack every time a table cell is selected.
This is a sample of the tableview:didselectrowatindexpath: method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *nextController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// Here I setup some data from the singleton (just an example)
nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
So my question is does this approach have the potential to suffer performance loss? I'm worried about allocating and releasing memory every time the user selects a table cell. On the other hand, since I will potentially have a lot of data, I don't want to have to repeat this data by storing it in an array of controllers.
I could always just have my singleton prepare the view controllers and store those instead of just the raw data. But I find it a bit weird that the model is always implicit in the controller. Is there a proper way of having the model stored independently of controller knowledge?
How would you approach this situation?
Thanks in advance.
Your approach is right. In this line of code:
nextController.title = [mySingleton.titles objectAtIndex:[indexPath row]];
you are not "allocating and releasing" any data but the pointer's data. nextController.title
will point to some element inside mySingleton.titles
. The data lives only inside your singleton's class as long as you don't call any copy
method on the arrays.
精彩评论