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