iphone - 如何对 JSON 项目进行排序/分组

标签 iphone ios xcode json uitableview

***更新问题****** JSON 数据是整个星期的体育课。

目前我正在下载 JSON 数据并将其保存到一个 NSObject SearchResult,然后在我的 UItableviewController 上显示每个体育课一行。

但我想按天对项目(体育课)进行分组,并在自己的表格上显示每一天。

所以我的问题是我是否需要按日期“DAY_OF_WEEK”对项目进行分组并将每一天存储到自己的数组(MonArray ... SunArray)中?

或者像我现在做的那样收集所有数据,然后每天对数组进行分组?

我希望这是有道理的。

感谢您的帮助。

- (void)viewDidLoad {

[super viewDidLoad];

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: JsonURL];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES]; });  }

-(SearchResult *)parseTrack:(NSDictionary *)dictionary {
SearchResult *searchResult1 = [[SearchResult alloc] init];
searchResult1.day = [[dictionary objectForKey:@"post"] objectForKey:@"DAY_OF_WEEK"];
searchResult1.classType= [[dictionary objectForKey:@"post"] objectForKey:@"CLASS_TYPE"];
return searchResult1;
}


- (void)fetchedData:(NSData *)responseData { //parse out the json data

searchResults2 = [NSMutableArray arrayWithCapacity:10];

NSError* error;
NSDictionary* dictionary = [NSJSONSerialization
                      JSONObjectWithData:responseData //1 
                            options:kNilOptions error:&error];
NSArray *array = [dictionary objectForKey:@"posts"];

NSLog(@"array : %@",array);

if (array == nil) {
    NSLog(@"Expected 'posts' array");
    return;
}
for (NSDictionary *resultDict in array) {

    SearchResult *searchResult3;

    searchResult3 = [self parseTrack:resultDict];

    if (searchResult3 != nil) {
        [searchResults2 addObject:searchResult3];
    }

    NSLog(@"day: %@, class: %@", [[resultDict objectForKey:@"post"] objectForKey:@"DAY_OF_WEEK"], [[resultDict objectForKey:@"post"] objectForKey:@"CLASS_TYPE"]);

}

[self.tableView reloadData];


}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


 #pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {

// Return the number of sections.
return 1;
}

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {

// Return the number of rows in the section.
return searchResults2.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

SRCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SRCell1"];


// Configure the cell...


SearchResult *searchResult1 = [searchResults2 objectAtIndex:indexPath.row];
cell.daynameLabel.text = searchResult1.day;

return cell;
}

JSON输出NSlog

2012-04-26 06:12:51.256 passingdata[91699:fb03] array : (
    {
    post =         {
        "CLASS_LEVEL" = "Intro/General";
        "CLASS_TYPE" = "Muay Thai";
        "DAY_OF_WEEK" = Friday;
        ID = 19;
        "ORDER_BY" = 5;
        TIME = "1:00pm - 2:30pm";
    };
}
    {
    post =         {
        "CLASS_LEVEL" = "General/Intermediate/Advanced";
        "CLASS_TYPE" = "Muay Thai Spar - Competitive";
        "DAY_OF_WEEK" = Friday;
        ID = 27;
        "ORDER_BY" = 5;
        TIME = "6:00pm - 9:00pm";
    };
},
    {
    post =         {
        "CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
        "CLASS_TYPE" = "Fighters Training";
        "DAY_OF_WEEK" = Monday;
        ID = 1;
        "ORDER_BY" = 1;
        TIME = "9:30am - 11:00pm";
    };
},

最佳答案

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DAY_OF_WEEK" ascending:TRUE];
[sourceArr sortUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];

希望这对你有帮助。

关于iphone - 如何对 JSON 项目进行排序/分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10327749/

相关文章:

iphone - iPhone 中的自动旋转问题(纵向到横向)

ios - 如果设备版本为 iOS 8,则删除启动屏幕

iOS 10 问题 : UIScrollView Not Scrolling, 即使设置了 ContentSize

ios - 获取调用的 JSON 函数时复制数据

iphone - webview 中阅读页面的自定义 Split View

xcode - 如何使用swift从VCard中检索数据

ios - 哪一行代码决定 iOS 项目使用 Storyboard或 Nib 或两者都不使用?

iphone - UIAlertView 每次调用都会弹出三次,而不是一次

ios - 是通过编程方式将设备iPhone或iPad

ios - 使用警报按钮更改警报消息而不关闭