【问题标题】:Reading plist into TableView将 plist 读入 TableView
【发布时间】:2010-12-10 13:36:27
【问题描述】:

我从一个包含两个字符串数组的字典的简单 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 值以添加字幕的具体代码是什么。我已经为此工作了几天,并阅读了参考资料和大量示例,但不幸的是无法解决问题。非常感谢您的帮助。

【问题讨论】:

    标签: iphone uitableview plist nsmutabledictionary


    【解决方案1】:

    因此,如果您将 Standard - Array 存储在您在 .h 文件中定义的 Array 中,那么这样的方法就可以了。在这个例子中,数组是根据 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];
    }
    

    希望能帮上忙。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多