从JSON为UITableView排序NSDictionary

S A*_*mik 3 iphone objective-c uitableview ios

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

NSDictionary数据来自JSON,数据已经从服务器排序,并在JSON中按顺序显示,但是当它被检索并转换为NSDicitionary时,顺序完全错误......

这是JSON

{ 
  "categories":{
   "unknownCategoryName":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName1":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName2":[
   { "key1":"value1",
     "key2":"value2"
   }],
   "unknownCategoryName3":[
   { "key1":"value1",
     "key2":"value2"
   }]
  }
 }
Run Code Online (Sandbox Code Playgroud)

在收到JSON之前,将不知道类别数量及其名称,因此这是我用于获取计数并设置tableView和部分的内容

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;
 }
Run Code Online (Sandbox Code Playgroud)

而我想要做的是列出部分和部分标题作为JSON中的顺序....而不是像一个混乱显示像

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

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

所以,如果你们能提供帮助,那就太棒了.提前致谢...

rde*_*mar 8

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

在编辑后回答另一个问题:

要使用已排序的数组来获取您想要的值,我认为这应该是接近的.您必须在.h文件中添加一个属性NSArray*sorted,然后在.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;
    }
Run Code Online (Sandbox Code Playgroud)

你没有说你想如何在一个表行中显示两个不同的值 - 在这个例子中,我将它们连接成一个字符串,并在两个表之间返回.