Sie könnten verschachtelte Arrays anstelle von verschachtelten Wörterbüchern erstellen. Und erstellen Sie ein weiteres Array, das den Namen des Staates enthält.
- Root (array)
- Texas (array)
- Location1 (dictionary)
- TitleOfLocation (string)
- Address (string)
- ...
- Florida (array)
- Location1 (dictionary)
- ...
- Location2 (dictionary)
- ...
// and
- StateNames (array)
- Texas (string)
- Florida (string)
Aus praktischen Gründen könnten Sie diese beiden Arrays in ein einzelnes NSDictionary einfügen. Das habe ich in meinem Beispiel nicht gemacht.
Und weil NSArray, NSDictionary und NSString alle zum NSCoding Protokoll bestätigen können Sie dieses Wörterbuch auf die Festplatte mit writeToFile schreiben: atomar:
ich für Sie einige Beispiel-Code geschrieben, weil ich die Lösung denken, ist ziemlich einfach, aber schwer mit Worten beschreiben.
#define kKeyDescription @"kKeyDescription"
#define kKeyAddress @"kKeyAddress"
#define kKeyCity @"kKeyCity"
#define kKeyState @"kKeyState"
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *root = [NSMutableArray array];
NSMutableArray *sectionTitle = [NSMutableArray array];
NSMutableArray *florida = [NSMutableArray array];
[root addObject:florida];
[sectionTitle addObject:@"Florida"];
NSMutableArray *texas = [NSMutableArray array];
[root addObject:texas];
[sectionTitle addObject:@"Texas"];
NSDictionary *location1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"My favorite pizza place", kKeyDescription,
@"5106 Grace Drive", kKeyAddress,
@"Miami", kKeyCity,
@"Florida", kKeyState,
nil];
[florida addObject:location1];
NSDictionary *location2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"Home", kKeyDescription,
@"1234 Foobar Street", kKeyAddress,
@"Fort Lauderdale", kKeyCity,
@"Florida", kKeyState,
nil];
[florida addObject:location2];
NSDictionary *location3 = [NSDictionary dictionaryWithObjectsAndKeys:
@"Franks Workplace", kKeyDescription,
@"9101 Baz Avenue", kKeyAddress,
@"Houston", kKeyCity,
@"Texas", kKeyState,
nil];
[texas addObject:location3];
data = [root retain];
sectionData = [sectionTitle retain];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.data count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.sectionData objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.data objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellID = @"MyCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
}
NSDictionary *object = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
// < This is the array for the selected state >
cell.textLabel.text = [object valueForKey:kKeyDescription];
cell.detailTextLabel.text = [object valueForKey:kKeyAddress];
return cell;
}
Ich bin ziemlich sicher, dass Sie einfach Ihre Wörterbücher in Code verschachteln können. dh. '[stateNameDictionary setObject: LocationDictionary forKey: @" TitleOfLocation "];' – mackross