I have a NSMutableDictionary created from a JSON query, running the json query in a browser has the output ordered alphabetically as i need it, and displaying this in the correct order using NSLOG verifies this. However, when I populate the UITableView cells the order comes out completely differently yet I wish to preserve the original order.
I understand a dictionary is not designed to be ordered and i can map to a new sorted array however if i do this (if this is the right way to achieve this?) it's unclear how to address the right keys and index for the detail view. Any thoughts? Thanks.
The code for creating the JSON data and creating the table cells is below:
- (void) makeData {
//Define dictionary
fullDictionary = [[NSMutableDictionary alloc] init];
//parse JSON data from a URL into an NSArray
NSString *urlString = [NSString stringWithFormat:@"http://<JSON feed goes here>"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
fullDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// cell data - extracting the appropriate values by object rows
cell.textLabel.text = [[[fullDictionary allValues] valueForKeyPath:@"strTerm"] objectAtIndex:indexPath.row];
return cell;
}