将 plist 读入 TableView

发布于 2024-10-06 21:13:49 字数 1387 浏览 3 评论 0原文

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

I started this project with a simple plist of a dictionary with two arrays of strings. I now want to add more information and want to use this plist structure:

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

Sorry about typing in the plist but the site would not let me post an image

I know that the RGB values could be numbers instead of strings but I have a reason for them being strings.

This is the code I used to read the simple 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;
}

My question is what is the specific code to retrieve the contents of the color dictionaries to get the Colors for the cell.textLabel.text and also read the RGB values to add a subtitle. I've been working on this for several days and have read references and lots of examples and unfortunately can't solve the problem. Your assistance will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-10-13 21:13:50

因此,只要您将标准数组存储在 .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];
}

希望这能帮上忙。

So providing you have your Standard - Array stored against a Array you've defined in your .h file then something like this would work. In this example the array is stored against 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];
}

Hopefully that'll give a a hand.

长发绾君心 2024-10-13 21:13:50

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

First, do not use -[UITableViewCell initWithFrame:reuseIdentifier:]. It has been deprecated and will give you a warning, plus it will make your subtitle harder to implement. This code is a modified version of yours which loads the information, sets the title to the Color property, and sets the subtitle to a string containing the Rvalue, Gvalue, and Bvalue properties.

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文