【问题标题】:UITableView Plist Not Returning valueForKeyUITableView Plist 不返回 valueForKey
【发布时间】:2010-09-23 00:28:36
【问题描述】:

在将分段 plist 实现到 UITableView 中时,我陷入了困境。

我的 plist 设置如下。我有一个根数组,其中包含表示部分的字典。这些字典有一个单独的字符串“sectionname”,它指示节标题以及内部的字典数组来表示单元格(这样做的原因是允许对单元格进行计数并且仍然具有节标题)。

Root - Array
   Item 0 - Dictionary  
       sectionname - string - A
       RowsInSection - Array
          Item 0  - Dictionary
             name - string - Adam
          Item 1 - Dictionary
             name - string - Andrew
   Item 1 - Dictionary
       sectionname - string - B
       RowsInSection - Array
          Item 0 - Dictionary
             name - string - Belle
          Item 1 - Dictionary
             name - string - Bob
   Item 3 - Dictionary

[等等]。

现在,一切似乎都在工作,正确命名的标题在那里,正确的行数在它们的部分下。但是,这些行没有标签,对于我的生活,我无法弄清楚为什么。

我有一个 plist 的 NSMutableArray

    libraryContent = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"]];

section中的行数就是这样。

NSInteger count = [[[[doa libraryContent] objectAtIndex:section] valueForKeyPath:@"RowsInSection.@count"]intValue];
return count;

还有标题的标题。

return [[[doa libraryContent] objectAtIndex:section] objectForKey:@"sectionname"];

我尝试了以下方法无济于事。

cell.textLabel.text = [[[doa libraryContent] objectAtIndex:indexPath.row] valueForKey:@"name"];

我还在学习,这是从不同的地方拼凑起来的。 'doa' 方法链接到另一个声明了 'libraryContent' 的文件。

你们知道如何从 plist 中取出名字吗?或任何其他方法来做我想做的事?

【问题讨论】:

    标签: iphone objective-c xcode uitableview


    【解决方案1】:

    试试这个代码:

    cell.textLabel.text = [[[[[doa libraryContent] objectAtIndex:indexPath.section] objectForKey:@"RowsInSection"] objectAtIndex:indexPath.row] objectForKey:@"name"];
    

    编辑
    这样代码将更具可读性:

    NSDistionary *sectionDataDict = [[doa libraryContent] objectAtIndex:indexPath.section];
    NSArray *sectionRowsArray = [sectionDataDict objectForKey:@"RowsInSection"];
    NSDictionary *rowDataDict = [sectionRowsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [rowDataDict objectForKey:@"name"];
    

    【讨论】:

    • 感谢您,它运行良好。我已经尝试了一百万种不同的变体,而且我 - 这 - 接近解决它。再次感谢您的帮助。
    • 很高兴能为您提供帮助。如果您能接受答案,我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多