开发者

How to modify iPhone plist to add another level and access data in code

I have a plist with an alphabetical indexed list of entries. I now want to add a further level so that each entry has a definition text (like a small dictionary.) I need some advice as to how to modify my plist and code to accommodate this. The existing code is below - it's from the Apress book, but I can't seem to modify it for another level.

NSString *path = [[NSBundle mainBundle] pathForResource:@"sortedglossary" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
self.names = dict;
[dict release];

NSArray *array = [[names allKeys] sortedArr开发者_StackOverflowayUsingSelector:@selector(compare:)];
self.keys = array;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionInTable start");
return [keys count];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection start");
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];
}

cell.text = [nameSection objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSLog(@"titleForHeader");
NSString *key = [keys objectAtIndex:section];
return key;
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return keys;
}


There are a number of ways to accomplish this. Normally I will just create another child of the root node in the plist for definitions.

So your plist would have the array of your entries and it would have another dictionary. The dictionary contains the definition text, keyed by the values from the first array. That way, you can just look up the definition when you need it.

So the structure would look roughly like this:

Root
  Entries(NSArray)
    0 => entryA(NSString)
    1 => entryB(NSString)
    ...
    x => entryX(NSString)
  Definitions(NSDictionary)
    entryA(NSString) => definitionA(NSString)
    entryB(NSString) => definitionB(NSString)
    ...
    entryX(NSString) => definitionX(NSString)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜