2012-04-26 26 views
0

***更新问题 ** * *** JSON数据是所有星期体育课。如何分页/分组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"; 
    }; 
}, 
+0

你可以检查这个获得有关如何使用sortDescriptor对数组进行排序的想法,那么你将很容易能够修复它 - http://stackoverflow.com/questions/805547/how-to-sort-an- nsmutablearray -with-custom-objects-in-it – rishi 2012-04-26 05:39:02

+0

我没有得到你在这里需要..你想解析json或其他任何东西 – vishy 2012-04-26 05:39:15

+0

我刚刚更新了问题..感谢 – HernandoZ 2012-04-26 09:24:54

回答

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

也许这会帮助你。

+0

谢谢我刚刚更新了我的问题。 – HernandoZ 2012-04-26 09:24:43