Я хочу сделать табличное представление, содержащее наши магазины, разделенные городами.Динамические заголовки для разделов в UITableView
Данные для просмотра таблицы собраны через API моего webapp в json-строке, которую я разбираю в NSDictionary, используя рамки Objective-c JSON с открытым исходным кодом.
Ответ выглядит как это:
{
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";
},
Мой первоначальный план был создать один массив за город, хранить эти массивы в словаре и чем, при именовании разделов с titleForHeaderInSection:
сделать что-то вроде этого:
if (section == 0) {
return @"Amsterdam"; }
else if (section == 1) {
return @"Roteterdam";
} Etc..
Вот проблема: мы довольно быстро расширяется, и я не хочу обновлять приложение каждый раз мы открываем магазин в новом городе. Поэтому я не могу жестко задавать массивы для городов.
Что было бы правильным способом убедиться, что новые магазины в новых городах отображаются в виде таблицы в нужном городе?
Заранее благодарен!
EDIT Вот мой код:
- (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;
}
Тема должна быть UITableView, не так ли? –
К сожалению, вы правы. Редактировать .. –