【发布时间】:2013-05-07 12:12:27
【问题描述】:
在我的 json 文件中,我有一个 title、subtitle 和 url。
我对@987654324@ 进行排序以按字母顺序设置项目,但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:的实现吗? -
我已经在上面插入了你想要的tableview。
-
我不确定我是否理解这个问题,但很明显你没有从
jsonStations得到正确的url,因为你排序jsonArray而你没有排序@987654336 @。换句话说,数组不再具有相同的排序顺序。使用arrayOfItems作为表的数据源不是更容易吗?拥有两个必须在排序方面保持同步的单独数组并不是一个好主意。有可能,但这不是一个好主意。
标签: json sorting ios6 nsjsonserialization