从JSON为UITableView排序NSDictionary [英] Sorting NSDictionary from JSON for UITableView

查看:106
本文介绍了从JSON为UITableView排序NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试按顺序排序我的NSDictionary时遇到了麻烦,而且已经有好几周了,我仍然没有想到它,所以我希望有人可以帮我一把......

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数据来自JSON,数据已经从服务器排序,并按JSON顺序显示,但是当它被检索并转换为NSDicitionary时,顺序完全错误......

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...

这是JSON

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

在收到JSON之前,不会知道类别数量及其名称,因此t他是我用来获取计数和设置tableView和部分

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

我想要做的是列出部分和部分标题作为JSON中的顺序....而不是像

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

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

unknownCategory是产品类别名称,它们不是字母顺序,没有任何数字,它们已经排序并按顺序显示在JSON ...

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...

推荐答案

在NSDictionaries中没有订购密钥,因此您必须先对密钥进行排序在表视图中使用它们之前。因此,不是使用[self.datasource allKeys]来获取节标题,而是首先创建一个排序数组:sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare :)],然后使用该数组来获取titles:title = [sorted objectAtIndex:section]。

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:

使用排序数组获取您想要的值你的桌子,我觉得这应该很接近。您必须在.h文件中添加一个属性NSArray * sorted,然后在.m中添加(这假设您的json的结构与上面发布的一样):

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

您没有说明如何在一个表格中显示两个不同的值row - 在这个例子中,我将它们连接成一个字符串,并在两者之间返回。

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.

这篇关于从JSON为UITableView排序NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆