I want to make a tableview containing our stores – sectioned by city.
The data for the tableview is collected thru my webapp’s API in a json string which I parse to a NSDictionary using the opensource Objective-c JSON framework.
The response looks a bit like this:
{
city = "Amsterdam";
code = erbur;
title = "Shop Lorem";
},
{
city = "Amsterdam";
code = kadap;
title = "Shop Ipsum";
},
{
city = "Rotterdam";
code = elaml;
title = "Shop Dolor";
},
{
city = "Delft";
code = lamla;
title = "Shop Sit";
},
{
city = "Rotterdam";
code = koppa;
title = "Shop Amet";
},
My initial plan was to create one array per city, store those arrays in a dictionary and than, when naming the sections with titleForHeaderInSection: do something like this:
if (section == 0) {
return @"Amsterdam"; }
else if (section == 1) {
return @"Roteterdam";
} Etc..
Here’s the problem: We are expanding quite rapidly and I don’t want to update my app everytime we open a shop in a new city. So I can’t hardcode the arrays for the cities.
What would be the right way to make sure that new stores in new cities are displayed in the table view within the right city?
Thanks in advance!
EDIT
Here’s my code:
- (void)getLocData
{
SBJSON *parser = [[SBJSON alloc] init];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://foo.bar/locaties/lijst"]];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
//the abive works
[statusesArray release];
statusesArray = statuses;
NSArray *cityNames = [statuses valueForKey:@"code"];
[locationCodes release];
locationCodes = cityNames;
- (void)viewDidLoad {
[super viewDidLoad];
[self getLocData];
//titel
self.title = NSLocalizedString(@"Locaties", @"Locaties - Steden");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [locationCodes count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [locationCodes objectAtIndex:section];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSString *cityName = [locationCodes objectAtIndex:[indexPath section]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"code like %@", cityName];
NSArray *filteredShops = [statusesArray filteredArrayUsingPredicate:predicate];
NSDictionary *currentShop = [filteredShops objectAtIndex:[indexPath row]];
NSString *cellTitle = [currentShop objectForKey:@"title"];
cell.textlabel.text = cellTitle;
return cell;
}
It looks as though your data ends up being an array of dictionaries. If that’s the case you can obtain an array of city names as follows:
So your
tableView:numberOfSections:implementation could simply return a count of that array, and similarlytableView:titleForHeaderInSection:could just return the object incityNamesindexed by the section number.In
tableView:cellForRowAtIndexPath:, you could then filter the array using an instance ofNSPredicate:EDIT
Assuming you just wanted to display each shop’s title, all you’d need is the following: