0
在我的json文件中,我有一个title
,subtitle
和url
。iOS6排序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中匹配url
和title
?
任何帮助表示赞赏
编辑:这里的的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:'的实现吗? – 2013-05-11 08:12:15
我已经插入了你想要的桌面视图。 – 2013-05-13 06:42:44
我不确定我是否理解这个问题,但似乎很明显,您没有从'jsonStations'返回正确的url,因为在不对'jsonStations'进行排序的同时对'jsonArray'进行排序。换句话说,数组不再具有相同的排序顺序。使用'arrayOfItems'作为你的表的数据源不是更容易吗?有两个单独的数组,你不得不保持同步的排序方式不是一个好主意。这是可能的,但这不是一个好主意。 – 2013-05-13 15:23:18