【问题标题】:Sectioned UITableView using NSMutableArray of NSDictionaries使用 NSDictionaries 的 NSMutableArray 分割 UITableView
【发布时间】:2014-03-18 22:08:48
【问题描述】:

我有一个 NSMutableArray 的 NSDictionaries,登录时看起来像这样

<__NSArrayM 0x165bcfc0>(
{
    SeqN = 122;
    Code = 024;
    Number = 1;
},
{
    SeqN = 132;
    Code = 030;
    Number = 1;
},
{
    SeqN = 124;
    Code = 002;
    Number = 2;
},
{
    SeqN = 114;
    Code = 035;
    Number = 3;
},
{
    SeqN = 144;
    Code = 009;
    Number = 3;
},
{
    SeqN = 444;
    Code = 022;
    Number = 3;
}
)

我想知道使用数字作为部分创建 UITableView 的最佳方法,所以 UITableView 看起来像这样

Number 1
SeqN 122, Code 024
SeqN 132, Code 030
Number 2
SeqN 124, Code 002
Number 3
SeqN 114, Code 035
SeqN 144, Code 009
SeqN 444, Code 022

我只是迷路了,我不确定是否需要将 NSDictionaries 的 NSMutableArray 溢出到数组数组中?或者有没有更简单的解决方案可以使用我不知道的东西来实现?

我已经设法创建了一个唯一的 Number 数组,用于我的 sectionCount 返回。我不知道如何返回每个部分的行数或拆分数组以便显示正确的数据。

更新

这就是我如何从我的 NSMutableArray 的 NSDictionaries 创建一个 NSArrays 的 NSDictionary

 NSMutableDictionary *sortedDictionaryArraysForTableView = [NSMutableDictionary dictionary];

    for (NSDictionary *dict in xmlMArray) {
        NSString *currNumber = [dict objectForKey:@"Number"];
        if(![[sortedDictionaryArraysForTableView allKeys] containsObject:currNumber])
            sortedDictionaryArraysForTableView[currNumber] = [NSMutableArray array];
        [sortedDictionaryArraysForTableView[currNumber] addObject:dict];
    }

【问题讨论】:

    标签: ios iphone objective-c nsmutablearray nsdictionary


    【解决方案1】:

    将您的字典数组转换为字典数组的字典,其中键是 Numbers,值是包含 Number 的所有字典的数组。

    字典的count 现在是节数。您可以使用allKeys 来获取所有数字(并对其进行排序,以便获取部分键和相关的项目数组)。

    【讨论】:

      【解决方案2】:

      重新组织您的数据,使其如下所示:

      @[
           @{
              @"number":@(1),
              @"items":@[
                  @{ @"seqn": @"114", @"code": @"035", @"number:@(1) },
      ....
           @},
      @]
      

      基本上你想要一个字典数组,每个字典都包含一个字典数组(每个都代表你当前的项目)

      【讨论】:

      • 另外,您可以使用@wain 的答案,主要区别在于这会产生更复杂的数据,但在字典没有的地方保持顺序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      相关资源
      最近更新 更多