2013-05-07 82 views
0

在我的json文件中,我有一个title,subtitleurliOS6排序JSON对象

我对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的第一个项目,我得到一个总指出错误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]; 
} 
+0

你可以发布你的'tableView:didSelectRowAtIndexPath:'的实现吗? – 2013-05-11 08:12:15

+0

我已经插入了你想要的桌面视图。 – 2013-05-13 06:42:44

+0

我不确定我是否理解这个问题,但似乎很明显,您没有从'jsonStations'返回正确的url,因为在不对'jsonStations'进行排序的同时对'jsonArray'进行排序。换句话说,数组不再具有相同的排序顺序。使用'arrayOfItems'作为你的表的数据源不是更容易吗?有两个单独的数组,你不得不保持同步的排序方式不是一个好主意。这是可能的,但这不是一个好主意。 – 2013-05-13 15:23:18

回答

0

的代码放在错误的,另一个设置的代码,固定的排序问题。