iOS6对JSON对象进行排序
在我的json文件中,我有一个iOS6对JSON对象进行排序,json,sorting,ios6,nsjsonserialization,Json,Sorting,Ios6,Nsjsonserialization,在我的json文件中,我有一个标题,副标题,和url 我对标题进行排序以按字母顺序设置项目,但是url没有与标题排序,我不知道为什么 这就是我所做的: NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; NSArray *arrayOfItems = [allDataDict
标题
,副标题
,和url
我对标题
进行排序以按字母顺序设置项目,但是url
没有与标题
排序,我不知道为什么
这就是我所做的:
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
。我应该怎么做才能使标题与tableView中的url
和title
相匹配
谢谢你的帮助
编辑:这是表视图: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:DidSelectRowatineXpath:
的实现吗?我已经插入了你想要的上面的tableView。我不确定我是否理解这个问题,但是很明显,您没有从jsonStations
中获得正确的url,因为您对jsonArray
进行排序,而不对jsonStations
进行排序。换句话说,数组不再具有相同的排序顺序。使用arrayOfItems
作为表的数据源不是更容易吗?拥有两个独立的阵列并不是一个好主意,您必须在排序方面保持同步。这是可能的,但不是一个好主意。