iOS6排序JSON对象

时间:2013-05-07 12:12:28

标签: json sorting ios6 nsjsonserialization

在我的json文件中,我有一个titlesubtitleurl

我对title进行排序以按字母顺序设置项目,但url未按title排序,我不知道原因。

这就是我所做的:

NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSArray *arrayOfItems = [allDataDictionary objectForKey:@"items"];

    for (NSDictionary *diction in arrayOfItems) {
        NSString *titles = [diction objectForKey:@"title"];
        NSString *station = [diction objectForKey:@"url"];

        [jsonArray addObject:titles];
        [jsonStations addObject:station];

// SORT JSON
        NSArray *sortedArray;
        sortedArray = [jsonArray sortedArrayUsingComparator:^NSComparisonResult(NSString *title1, NSString *title2)
                       {
                           if ([title1 compare:title2] > 0)
                               return NSOrderedDescending;
                           else
                               return NSOrderedAscending;
                       }]; 
        [jsonArray setArray:sortedArray];

如果我按下tableView中的第一项,我会从总差异url获得title。我该怎么做才能使标题与tableView中的urltitle匹配?

任何帮助表示赞赏

编辑:这里是tableView:didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if(indexPath.row == _currentRadio) {
        return;
    }

    if(_radio) {
        [_radio shutdown];
        [_radio release];
        _radio = nil;
    }

    [_statusLabel setText:@""];
    [_titleLabel setText:@""];

    _currentRadio = indexPath.row;
    NSString *radioUrl = [jsonStations objectAtIndex:indexPath.row];
    if([radioUrl hasPrefix:@"mms"]) {
        _radio = [[MMSRadio alloc] initWithURL:[NSURL URLWithString:radioUrl]];
    } else {
        _radio = [[HTTPRadio alloc] initWithURL:[NSURL URLWithString:radioUrl]];
    }

    if(_radio) {
        [_radio setDelegate:self];
        [_radio play];
    }



    [self.tableview reloadData];
}

1 个答案:

答案 0 :(得分:0)

代码放错了,代码的另一个设置,修复了排序问题。