Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

iphone - Sorting NSDictionary from JSON for UITableView

I am having trouble trying to sort my NSDictionary in order, and it's been weeks and I still haven't figured it out, so I wish some one could give me a hand here...

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

This is the JSON

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

The category quantity and its name will not be known until the JSON is received, so this is what I am using to get the count and setting up the tableView and the 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 to list out the section and the section title as the order in the JSON.... rather than a chaos display like

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

The "unknownCategory" is Product Category name, they are not in alphabet order, without any number, and they are already sorted and display in order in the JSON...

So, it would be great if you guys could help. Thanks in advance...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Keys are not ordered in NSDictionaries, so you have to sort the keys first before using them in your table view. So, instead of using [self.datasource allKeys] to get your section titles, first create a sorted array: sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)], and then use that array to get the titles: title = [sorted objectAtIndex:section].

After Edit to answer a further question:

To use the sorted array to get the values you want into your table, I think this should be close. You would have to add a property, NSArray *sorted, to your .h file, and then this in your .m (this is assuming the structure of your json is as you posted above):

 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:@"%@
%@",text1,text2];
        cell.textLabel.numberOfLines=0; // this set the number of lines to unlimited
        return cell;
    }

You didn't say how you wanted to display two different values in one table row -- in this example, I concatenated them into one string with a return between the two.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...