중첩 된 사전 대신 중첩 된 배열을 만들 수 있습니다. 그리고 상태의 이름을 보유하는 다른 배열을 만듭니다.
- Root (array)
- Texas (array)
- Location1 (dictionary)
- TitleOfLocation (string)
- Address (string)
- ...
- Florida (array)
- Location1 (dictionary)
- ...
- Location2 (dictionary)
- ...
// and
- StateNames (array)
- Texas (string)
- Florida (string)
편의상이 두 배열을 단일 NSDictionary에 넣을 수 있습니다. 나는 나의 예에서 그렇게하지 않았다.
그리고있는 NSArray, NSDictionary에와있는 NSString 모두가 NSCoding 프로토콜 확인하기 때문에 당신은 writeToFile와 디스크에이 사전 쓸 수 있습니다 : 나는이 솔루션은 매우 간단합니다 생각하기 때문에,
내가 당신을 위해 몇 가지 예제 코드를 작성하지만, 하드 : 원자를 단어로 설명하기.
#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;
}
저는 사전에 코드를 중첩 할 수 있다고 확신합니다. 즉. '[stateNameDictionary setObject : LocationDictionary forKey : @ "TitleOfLocation"]; ' – mackross