简体   繁体   English

使用NSDictionaries的NSMutableArray的UITableView节

[英]Sectioned UITableView using NSMutableArray of NSDictionaries

I have an NSMutableArray of NSDictionaries that looks like this when logged 我有NSDictionaries的NSMutableArray,在登录时看起来像这样

<__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;
}
)

I would like to know the best way to create a UITableView using the Number as sections, so the UITableView would look like this 我想知道使用Number作为部分来创建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

I am just lost, I am not sure if I need to spilt the NSMutableArray of NSDictionaries into an Array of Arrays? 我只是迷路了,我不确定是否需要将NSDictionaries的NSMutableArray溢出到Arrays中? Or is there an easier solution that can be achieved using something I don't know? 还是有一个我不知道的东西可以实现一个更简单的解决方案?

I have managed to create a Unique array of Number that I am using for my sectionCount return. 我设法创建了一个唯一的Number数组,用于我的sectionCount返回。 I don't know how to return how many rows per section or split the array so the correct data is being displayed. 我不知道如何返回每节多少行或拆分数组,以便显示正确的数据。

Update 更新

This is how I created an NSDictionary of NSArrays from my NSMutableArray of NSDictionaries 这就是我从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];
    }

Convert your array of dictionaries into a dictionary of arrays of dictionaries where the keys are the Number s and the values are an array of all of the dictionaries which contain that Number . 将您的字典数组转换成字典字典数组,其中键是Number ,值是包含该Number的所有字典的数组。

The count of the dictionaries is now the number of sections. count的字典现在是区段的数量。 You can use allKeys to get all of the numbers (and sort it so you can get the section key and associated array of items). 您可以使用allKeys来获取所有数字(并对其进行排序,以便可以获取section键和相关的项数组)。

Reorganize your data so that it looks like: 重新整理您的数据,使其看起来像:

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

basically you want an array of dictionaries, each containing an array of dictionaries (each of which represents your current item) 基本上,您需要一个字典数组,每个字典包含一个字典数组(每个字典代表您当前的项目)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM