Sort NSDictionary from JSON to UITableView

I had problems sorting my NSDictionary in order, and it has been weeks, and I still haven’t figured it out, so I would like someone to give me a hand here ...

NSDictionary data is taken from JSON, and the data is already sorted from the server and displayed in JSON in order, but when it is retrieved and converted to NSDicitionary, the order is all wrong ...

This is json

{ 
  "categories":{
   "unknownCategoryName":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName1":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName2":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName3":[
   { "key1":"value1",
     "key2":"value2"
   }]
  }
 }

The number of categories and its name will not be known until JSON is received, so this is what I use to get the counter and set up the View table and section

NSDictionary *results = [jsonString JSONValue];

NSDictionary *all = [results objectForKey:@"categories"];
self.datasource = all;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
   return [self.datasource count];
  }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   NSString *title = [[self.datasource allKeys] objectAtIndex:section];
   return title;
 }

And what I want to do is list the section and section title as an order in JSON ... and not display chaos, for example

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

"unknownCategory" - , , - , JSON...

, , , , . ...

+3
4

NSDictionaries, . , , [self.datasource allKeys] , : sorted = [[self.datasource allKeys] sortedArrayUsingSelector: @selector (caseInsensitiveCompare:)], , title: title= [ AtIndex: section].

, :

, , , . , NSArray *, .h , .m(, json , ):

 NSDictionary *results = [jsonString JSONValue];
    NSDictionary *all = [results objectForKey:@"categories"];
    self.datasource = all;
    self.sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return [self.sorted count];
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        NSString *title = [self.sorted objectAtIndex:section];
        return title;
    }

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [[self.datasource valueForKey:[self.sorted objectAtIndex:section]]count];
   }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        }
        NSArray *theArray = [self.datasource valueForKey:[self.sorted objectAtIndex:indexPath.section]];
        NSString *text1 = [[theArray objectAtIndex:0] valueForKey:@"key1"];
        NSString *text2 = [[theArray objectAtIndex:0] valueForKey:@"key2"];
        cell.textLabel.text = [NSString stringWithFormat:@"%@\r%@",text1,text2];
        cell.textLabel.numberOfLines=0; // this set the number of lines to unlimited
        return cell;
    }

, - .

+8

JSON.

{
"categories": [
    "unknownCategoryName": [
        {
            "key1": "value1",
            "key2": "value2"
        }
    ],
    "unknownCategoryName2": [
        {
            "key1": "value1",
            "key2": "value2"
        }
    ]
]
}

, .

JSON

-(NSMutableArray *)getAllKeys:(NSDictionary *)jsonDictionary{

       NSDictionary *all = [results objectForKey:@"categories"];
       NSMutableArray *allKeys = [all allKeys];
       NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
       NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; 

       [allKeys sortUsingDescriptors:sortDescriptors]; 
       [sorter release];

       return allkeys;
   }

   -(void)createSortedDictionary{
        NSMutableDictionary *dictionaryMutable=[NSMutableDictionary dictionary];
        for(NSString *string in allkeys){

           [dictionaryMutable setObject:[all objectForKey:string] forKey:string];
        }
     }

dictionaryMutable

+1

JSON -, ? . , . JSON :

{ 
  categories:[
   {unknownCategoryName:
   { key1:value1,
     key2:value2
   }},
   {unknownCategoryName2:
   { key1:value1,
     key2:value2
   }}
  ]
 }

NSArray, ( ):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
      return [categoriesArray count];
  }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
     NSDictionary *dict = [categoriesArray objectAtIndex:section];  
     NSString *title = [[dict allKeys] objectAtIndex:0];
     return title;
 }
0

I think the only way to maintain the original order is to first parse after creating a JSON string from the JSON data. If the JSON string structure is found as you publish, you can use NSScanner to search for strings between quotation marks, and then select only those that have [after the last quotation mark to be added to the array, which will be your ordered key array. Then you can create an NSDictionary using the JSON parser and get the values ​​by going through your ordered keys and sending them the value ForKey :.

0
source

All Articles