【问题标题】:iOS6 Sort JSON objectsiOS6 对 JSON 对象进行排序
【发布时间】:2013-05-07 12:12:27
【问题描述】:

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

我对@9​​87654324@ 进行排序以按字母顺序设置项目,但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 中的第一项,我会从完全不同的 title 中获得 url。我应该怎么做才能使标题与 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];
}

【问题讨论】:

  • 你能发布你对tableView:didSelectRowAtIndexPath:的实现吗?
  • 我已经在上面插入了你想要的tableview。
  • 我不确定我是否理解这个问题,但很明显你没有从jsonStations 得到正确的url,因为你排序jsonArray 而你没有排序@987654336 @。换句话说,数组不再具有相同的排序顺序。使用arrayOfItems 作为表的数据源不是更容易吗?拥有两个必须在排序方面保持同步的单独数组并不是一个好主意。有可能,但这不是一个好主意。

标签: json sorting ios6 nsjsonserialization


【解决方案1】:

代码放错了,重新设置代码,修复了排序问题。

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 2018-02-13
    • 2014-09-27
    • 2013-07-08
    • 2021-09-16
    • 2011-05-12
    • 2013-07-15
    • 2020-03-06
    相关资源
    最近更新 更多