我正在尝试关注此帖http://www.appcoda.com/ios-programming-index-list-uitableview/以使用UITableViewController
创建部分。
我使用两个NSMutableDictionaries
第一个(_curatedItemDictionary
)字典来存储来自Parse的键值对,第二个(_sectionsDictionary
)是将这些存储在字典中作为键和值存储为类别类型。
[_sectionsDictionary setObject:[obj objectForKey:@"Category"] forKey:_curatedItemDictionary];
我这样做是因为我无法将键存储为类别和值作为字典,因为字典不支持重复键。
我使用以下代码获取numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
for(NSString *key in [_sectionsDictionary allKeys]) {
[_cleanArray addObject:[_sectionsDictionary objectForKey:key]];
}
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:_cleanArray];
for (id item in set) {
NSInteger count = (unsigned long)[set countForObject:item];
[_rowForIndexArray addObject:[NSNumber numberWithInteger:count]];
}
_cleanSectionArray = [[NSSet setWithArray:_cleanArray] allObjects];
return [_cleanSectionArray count];
}
如何从上述信息中获取numberOfRowsInSection
?我如何知道特定部分正在迭代哪些项目?
以下是我用于从Parse.com数据
创建curatedItemDictionary
的代码
PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithString:self.classname]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
for (PFObject *obj in objects) {
self.curatedItemDictionary = [[NSMutableDictionary alloc]init];
[_curatedItemDictionary setValue:[obj objectForKey:@"ItemName"] forKey:@"ItemName"];
[_curatedItemDictionary setValue:[obj objectForKey:@"Price"] forKey:@"Price"];
[_sectionsDictionary setObject:[obj objectForKey:@"Category"] forKey:_curatedItemDictionary];
[self.tableView reloadData];
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];