排序类别(列),因为它们存储在 Parse 后端
Order the Categories (column) as they are stored in Parse backend
我正在尝试使用列 "Category" 数据填充来自 Parse 的餐馆数据作为项目列表的部分标题。但是,项目的部分标题会随机显示。我希望它们在存储在后端时显示出来。我试着做 [query 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;
}
我通过创建一个新的 NSMutableArray
修复了它。实际上我是从这个 post How can i get Original order of NSDictionary/NSMutableDictionary?
读到的
我在遍历 PFObjects [_categoryMutableArray addObject:[obj objectForKey:@"Category"]];
时添加了这一行,然后我在 titleForHeaderInSection
和 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;
}
我正在尝试使用列 "Category" 数据填充来自 Parse 的餐馆数据作为项目列表的部分标题。但是,项目的部分标题会随机显示。我希望它们在存储在后端时显示出来。我试着做 [query 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;
}
我通过创建一个新的 NSMutableArray
修复了它。实际上我是从这个 post How can i get Original order of NSDictionary/NSMutableDictionary?
我在遍历 PFObjects [_categoryMutableArray addObject:[obj objectForKey:@"Category"]];
时添加了这一行,然后我在 titleForHeaderInSection
和 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;
}