0

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;
}
  • Could you clarify what you are trying to accomplish? You mention that you would like to have restaurant data "to be displayed as they are stored in the backend", but when using the Parse data browser you can change the ordering however you like. You also mentioned sorting them alphabetically, is that not what you want? – Russell Oct 06 '15 at 20:01
  • Hi Russell... I'm unable to figure out in what order the section titles are being populated. I wanted to display the categories column as section headers in the order it is in the Parse browser. Here's the screenshot http://imgur.com/iZojWjv the section condiments should be displayed first and the rest should follow after that but the section headers are scrambled. – Rider Forest Oct 06 '15 at 20:13
  • 1
    If you need to preserve ordering you should be using an array. By definition, dictionaries are unordered structures and when you get the sections in `titleForHeaderInSection` there is no guarantee what the order will be. – Russell Oct 06 '15 at 20:20
  • Thanks Russell. True. lemme try to store them in an array. – Rider Forest Oct 06 '15 at 20:36
  • I'm unable to do this using Arrays. is there any other way to categorize my items ? like a new column to distinguish a set of items as starters, main course, desserts so on ? – Rider Forest Oct 06 '15 at 23:17
  • I'd say you should absolutely have some kind of categorization. You might also want to look into using a `UICollectionView` for each category and then when a user selects one you could load up all of those respective items – Russell Oct 07 '15 at 04:40
  • I fixed it by creating a new NSMutableArray with no duplicates and showed them in titleForHeaderInSection. Thanks Russell for pointing me in the right direction. – Rider Forest Oct 07 '15 at 04:53

1 Answers1

0

I fixed it by creating a new NSMutableArray. Actually i read it from this post How can i get Original order of NSDictionary/NSMutableDictionary?

I added this line while iterating through my PFObjects [_categoryMutableArray addObject:[obj objectForKey:@"Category"]]; and then i used the below lines in my titleForHeaderInSection and numberOfRowsInSection

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{        
    return [_categoryMutableArray objectAtIndex:section];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSArray *keys = [self.sectionsDictionary allKeys];
    id aKey = [_categoryMutableArray objectAtIndex:section];
    NSMutableArray *arr = [self.sectionsDictionary objectForKey:aKey];
    return (arr) ? arr.count : 0;
}
Community
  • 1
  • 1