views:

431

answers:

2

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. Thanks in advance........

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

NSArray *array = [[names allKeys] sortedArrayUsingSelector:@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;

}

A: 

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)
DougW
Thanks for this. There will be multiple entries for each letter of the alphabet (like a mini dictionary). So will it look like:Root ??A (Array) => Item 0 (Dictionary) => AFirstWord (Array) => AFirstdefinition (String), Item 1 (Dictionary) => ASecondWord (Array) => ASeconddefinition (String)B (Array) => Item 0 (Dictionary) => BFirstWord (Array) => BFirstdefinition (String), Item 1 (Dictionary) => BSecondWord (Array) => BSeconddefinition (String)
Maxwell Segal
Well there isn't really a "right" or "wrong" way to do it. All depends on the needs of your program. It sounds like your needs are having ordered entries and definitions for those entries. Having more than one entry per letter of the alphabet shouldn't be an issue with the method I described. The solution you propose would probably work but it seems quite complex, and nesting arrays/dictionaries is not terribly memory friendly. All depends on the needs of your particular application though.
DougW
I appreciate your help and time here. The point is I've got as far as I can and need some advice as to what's the best way to do it as the structure of plist when I need more than one level, like here, is that I frankly am lost.
Maxwell Segal
A: 

Hi !

I'm beginner and I try to understand... HOW I can make a request to show all data (from plist) in SAME category (NOT ALL !) for exemple : show only the movie have : genre = Action

Please help me ... Thanks a lot ! Excuse my bad english !

Is it in this code I must make it or an other ???

  • (id)initWithLibraryName:(NSString *)libraryName { if (self = [super init]) { libraryPlist = libraryName; libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"]];
Fraise