订购分类(列),因为它们存储在Parse后端中

时间:2015-10-06 19:46:21

标签: ios objective-c parse-platform nsmutabledictionary pfquery

我正在尝试从Parse填充餐馆的数据,并将“Category”数据列作为项目列表的部分标题。但是,项目的部分标题将随机显示。我希望它们显示为存储在后端。我试过[查询orderByAscending:@“Category”];按字母顺序对类别进行排序,结果没有变化。以下是我目前的实施......

-(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;
}

1 个答案:

答案 0 :(得分:0)

我通过创建新的NSMutableArray来修复它。实际上我是从这篇文章How can i get Original order of NSDictionary/NSMutableDictionary?

中读到的

我在迭代我的PFObject [_categoryMutableArray addObject:[obj objectForKey:@"Category"]];时添加了这一行,然后我使用了titleForHeaderInSectionnumberOfRowsInSection

中的以下行
- (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;
}