Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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]. </p> <p>After Edit to answer a further question:</p> <p>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):</p> <pre><code> 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; } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload