- (void)viewDidLoad
{
[super viewDidLoad];
self.carMakes = [[NSArray alloc]
initWithObjects:@"Chevy",
@"BMW",
@"Toyota",
@"Volvo",
@"Smart", nil];
self.carModels = [[NSArray alloc]
initWithObjects:@"Volt",
@"Mini",
@"Venza",
@"S60",
@"Fortwo", nil];
self.carImages = [[NSArray alloc]
initWithObjects:@"chevy_volt.jpg",
@"mini_clubman.jpg",
@"toyota_venza.jpg",
@"volvo_s60.jpg",
@"smart_fortwo.jpg", nil];
}
The above code is populated in a Table view. I want to give the user the option to choose a few cars and by doing so the name of the car, model and picture will be saved in another list. I don’t know where I should save the data the user chooses and how.
I want to be able to show the user chosen car list on a TableView later on.
- I understand that if I want to keep the newly created list I’ll need to convert the chosen info to a plist file or something like that but I’m not sure where to save the data before that.
You can use array or dictionary for this. But a better approach is to create an array of model classes to hold these data. This is best suited for MVC architecture. For example, you can create a model class as
Carwhich has properties,NSString *make,NSString *modelandUIImage *imagedeclared in it. If you want to populate the table view you need to create an array of these car model objects and use that in delegate methods to populate the cells. If you want to persist the data, you can either convert this into a dictionary form and then store it as a plist. For more details, check this tutorial from ray wenderlichA typical model object will look like,
Create array of model objects as,
While displaying you can just use
[carList objectAtIndex:indexPath.row].makeetc..or
When the user chooses some cars, save those model objects into a separate array and save it as plist.