iOS6排序JSON对象 [英] iOS6 Sort JSON objects
本文介绍了iOS6排序JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的json文件中,我有标题
,字幕
和 url
。
In my json file I have a title
, subtitle
, and url
.
我按标题
排序以按字母顺序设置项目,但 url
未按标题
排序,我不知道原因。
I sort the title
to set the items alphabetically, but the url
isn't sorted with the title
and I don't know why.
这就是我所做的:
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
相匹配?
What happens is, if I press the first item in the tableView, I get get the url
from a total diffrent title
. What should I do to get the title to match the url
and title
in the tableView?
感谢任何帮助
编辑:这里是tableView:didSelectRowAtIndexPath
here's the 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];
}
推荐答案
代码放错了,另一个代码设置,修复了排序问题。
The code was placed wrong, another setup for the code, fixed the sorting problem.
这篇关于iOS6排序JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文