I am trying to populate data of restaurants from Parse with the column "Category" data as section heading for the list of items. However the section titles for the items are getting displayed randomly. I want them to be displayed as they are stored in the backend. I tried doing [query orderByAscending:@"Category"]; which sorts the Categories alphabetically and has no change in the results. The below is my current implementation...
-(void) listAllItems {
PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithString:self.restaurantName]];
[query whereKey:@"DinnerLunch" containsString:self.lunchDinnerPreference];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *obj in objects) {
self.curatedItemDictionary = [[NSMutableDictionary alloc]init];
[_curatedItemDictionary setValue:[obj objectForKey:@"ItemName"] forKey:@"ItemName"];
[_curatedItemDictionary setValue:[obj objectForKey:@"Price"] forKey:@"Price"];
[_curatedItemDictionary setValue:[obj objectForKey:@"Category"] forKey:@"Category"];
[_curatedItemDictionary setValue:@"" forKey:@"ModifiableItems"];
_sectionsArray = [_sectionsDictionary objectForKey: [obj objectForKey:@"Category"]];
if(nil == _sectionsArray){
self.sectionsArray = [[NSMutableArray alloc]init];
}
[_sectionsArray addObject:_curatedItemDictionary];
[_sectionsDictionary setObject: _sectionsArray forKey: [obj objectForKey:@"Category"]];
NSLog(@"categories keys are %@", [self.sectionsDictionary allKeys]);
}
[self.tableView reloadData];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_sectionsDictionary count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSArray *keys = [self.sectionsDictionary allKeys];
return [keys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *keys = [self.sectionsDictionary allKeys];
id aKey = [keys objectAtIndex:section];
NSMutableArray *arr = [self.sectionsDictionary objectForKey:aKey];
return (arr) ? arr.count : 0;
}