1

我有一个带有按字母索引的条目列表的 plist。我现在想添加一个更进一步的级别,以便每个条目都有一个定义文本(就像一本小字典)。我需要一些关于如何修改我的 plist 和代码以适应它的建议。现有代码如下 - 它来自 Apress 书,但我似乎无法将其修改为另一个级别。

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;
}
4

1 回答 1

0

有很多方法可以做到这一点。通常我会在 plist 中创建另一个根节点的子节点来进行定义。

所以你的 plist 会有你的条目数组,它会有另一个字典。字典包含定义文本,由第一个数组中的值作为键。这样,您可以在需要时查找定义。

所以结构大概是这样的:

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)
于 2009-12-22T22:04:27.273 回答