将plist读入TableView

时间:2010-12-10 13:36:27

标签: 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 - 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.textLabel.text的颜色,并读取RGB值以添加字幕。我已经在这几天工作了,并阅读了参考文献和许多例子,遗憾的是无法解决问题。非常感谢您的协助。

2 个答案:

答案 0 :(得分:1)

因此,假设您已经在.h文件中定义的数组中存储了标准数据 - 那么这样的东西就可以了。在此示例中,数组存储在self.coloursArray。

- (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];
}

希望能够伸出援助之手。

答案 1 :(得分:1)

首先,不要使用 - [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;
}