使用NSDictionaries的NSMutableArray划分UITableView

问题描述 投票:0回答:2

我有一个NSMictionArray的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;
}
)

我想知道使用Number as部分创建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

我迷失了,我不确定是否需要将字典的NSMutableArray拆分为数组数组?或者是否有一个更容易的解决方案可以使用我不知道的东西来实现?

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

更新

这就是我从NSDictionaries的NSMutableArray创建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
2个回答
1
投票

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

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


0
投票

重新组织您的数据,使其看起来像:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.