我有一个从 JSON 查询创建的 NSMutableDictionary,在浏览器中运行 json 查询会根据我的需要按字母顺序排列输出,并使用 NSLOG 以正确的顺序显示它来验证这一点。但是,当我填充 UITableView 单元格时,顺序完全不同,但我希望保留原始顺序。
我知道字典不是为了排序而设计的,我可以映射到一个新的排序数组,但是如果我这样做(如果这是实现这一目标的正确方法?)目前还不清楚如何为详细视图处理正确的键和索引. 有什么想法吗?谢谢。
创建 JSON 数据和创建表格单元格的代码如下:
- (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;
}