Iphone 将plist读入TableView

Iphone 将plist读入TableView,iphone,uitableview,plist,nsmutabledictionary,Iphone,Uitableview,Plist,Nsmutabledictionary,我开始这个项目时使用了一个简单的plist,它是一个包含两个字符串数组的字典。我现在想添加更多信息,并想使用此plist结构: Root - Dictionary - (2 items) Standard - Array - (3 items) Item 0 - Dictionary - (4 items) Color - String - Red Rvalue - String - 255 Gvalue - S

我开始这个项目时使用了一个简单的plist,它是一个包含两个字符串数组的字典。我现在想添加更多信息,并想使用此plist结构:

Root - Dictionary - (2 items)
   Standard - Array - (3 items)
      Item 0 - Dictionary - (4 items)
           Color - String - Red
           Rvalue - String - 255
           Gvalue - String - 0
           Bvalue - String - 0
很抱歉输入plist,但该网站不允许我发布图像

我知道RGB值可以是数字而不是字符串,但我有一个理由认为它们是字符串

这是我用来阅读简单plist的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *key = [keys objectAtIndex:section];
    NSArray *colorSection = [colors objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

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

    cell.textLabel.text = [colorSection objectAtIndex:row];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
    return cell;
}

我的问题是,检索颜色字典的内容以获取cell.textlab.text的颜色以及读取RGB值以添加字幕的具体代码是什么。我已经在这方面工作了好几天,阅读了很多参考资料和例子,不幸的是,我无法解决这个问题。非常感谢您的帮助。

因此,如果您将标准数组存储在您在.h文件中定义的数组中,那么类似的操作就可以了。在本例中,数组是针对self.colorsarray存储的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

if(cell == nil){
    cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier: SectionsTableIdentifier] autorelease];
    }
NSString* ColourString = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Colour"];
NSString* rValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Rvalue"];
NSString* gValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Gvalue"];
NSString* bValue = [[self.coloursArray objectAtIndex:indexPath.row] valueForKey:@"Bvalue"];
cell.textLabel.text = ColourString;
NSString* subCellString = [NSString stringWithFormat:@"%@:%@:%@", rValue, gValue, bValue];
}

希望这能帮上忙。

首先,不要使用-[UITableViewCell initWithFrame:reuseIdentifier:]。它已被弃用,将给你一个警告,而且它将使你的字幕更难实现。这段代码是您的修改版本,它加载信息,将标题设置为Color属性,并将副标题设置为包含Rvalue、Gvalue和Bvalue属性的字符串

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];
    NSString *key = [keys objectAtIndex:section];
    NSArray *colorSection = [colors objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if(cell == nil) {
        cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: SectionsTableIdentifier] autorelease];
    }

    NSDictionary *color = [colorSection objectAtIndex:row];
    cell.textLabel.text = [color objectForKey:@"Color"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@, %@",[color objectForKey:@"Rvalue"],[color objectForKey:@"Gvalue"],[color objectForKey:@"Bvalue"]];
    [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; //add disclosure button to rows
    return cell;
}