So as I am a new to iPhone programming and trying to sort it all out, I have an issue where I'm trying to save a dictionary to a plist in the following format...
-Root
-StateName (Dictionary)
- TitleOfLocation (Dictionary)
-Address (String)
-City (String)
-State (String)
Maybe I'm completely off here or the logic needs to be changed, but in the end I need it to populate in a UITableView sorted by the state name.
Currently I can save data to a single dictionary and write it开发者_运维技巧 to a PLST and pull it into a UITableView (the basics, yes i know), but as I said, I am just having issues saving nested Dictionaries.
Would someone be able to guide me the right direction? Thanks.
You could create nested arrays instead of nested dictionaries. And create another array that holds the name of the state.
- Root (array)
- Texas (array)
- Location1 (dictionary)
- TitleOfLocation (string)
- Address (string)
- ...
- Florida (array)
- Location1 (dictionary)
- ...
- Location2 (dictionary)
- ...
// and
- StateNames (array)
- Texas (string)
- Florida (string)
For convenience you could put those two arrays into a single NSDictionary. I didn't do that in my example.
And because NSArray, NSDictionary and NSString all confirm to the NSCoding protocol you can write this dictionary to disk with writeToFile:atomically:
I wrote some sample code for you, because I think the solution is pretty straightforward, but hard to describe with words.
#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;
}
精彩评论