Ios NSFetchedResultsController-如何在cellForRowAtIndexPath中显示右侧部分行
我有一份JSON阅读的国家和场馆列表。它现在可以正确地读入,并且似乎可以存储关系Ios NSFetchedResultsController-如何在cellForRowAtIndexPath中显示右侧部分行,ios,objective-c,uitableview,core-data,magicalrecord,Ios,Objective C,Uitableview,Core Data,Magicalrecord,我有一份JSON阅读的国家和场馆列表。它现在可以正确地读入,并且似乎可以存储关系 // Read JSON NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"venues" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:jsonPath]; id json = [NSJSONSerialization JSONObject
// Read JSON
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"venues" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) {
NSLog(@"Error - %@", error);
} else {
//NSLog(@"JSON = %@", json);
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
/*
"USA": [
{
"venue": "Von Braun Center",
"city" : "Huntsville",
"state": "Alabama",
"capacity": 13760
},
*/
for (NSDictionary *dict in json) {
NSString *name = (NSString *) [dict objectForKey:@"name"];
NSArray *venueList = (NSArray *) [dict valueForKey:@"venues"];
[MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *c) {
Country *country = [Country createInContext:c];
[country setName:name];
NSMutableArray *listOfVenues = [NSMutableArray array];
NSLog(@"Country - %@", country.name);
for (NSDictionary *venueData in venueList) {
NSString *name = (NSString *) [venueData objectForKey:@"venue"];
NSString *city = (NSString *) [venueData objectForKey:@"city"];
NSString *state = (NSString *) [venueData objectForKey:@"state"];
//NSNumber *capacity = (NSNumber *) [NSNumber numberWithInt:[[venueData valueForKey:@"capacity"] intValue]];
Venue *v = [Venue createInContext:c];
[v setName:name];
[v setCity:city];
[v setState:state];
[v setCountry:country];
[listOfVenues addObject:v];
NSLog(@"Venue - %@, %@", v.name, v.country.name);
} // next
[country setVenues:[NSSet setWithArray:listOfVenues]];
} completion:^{
}];
} // next
dispatch_async(dispatch_get_main_queue(), ^{
[[NSManagedObjectContext defaultContext] saveNestedContexts];
NSUInteger entities = [Venue countOfEntities];
NSLog(@"Venues saved = %d", entities);
});
});
} // end if
这会生成一个场馆日志,如下所示
正在加载场馆数据
Country - USA
Venue - Von Braun Center, USA
Venue - Birmingham Convention Center, USA
Country - UK
Venue - O2 Arena, UK
Venue - MEN Arena, UK
到目前为止还不错
但当我进入实际的场馆视图时,它只是按字母顺序显示场馆,但它确实以正确的顺序显示了部分
我的代码是
// viewDidLoad
self.fetchedResultsController = [Venue fetchAllGroupedBy:@"country.name" withPredicate:nil sortedBy:@"name" ascending:YES];
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Venue *v = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = v.name;
}
但我不知道如何修复它
我想知道我做错了什么,以及如何修复它,使其在正确的部分显示正确的数据。在为场馆设置国家/地区后
[v setCountry:country];
这条线是多余的,可能会破坏你们的关系
[country setVenues:[NSSet setWithArray:listOfVenues]];
您只需设置一次关系。反向关系是自动生成的。我能够将各个部分放入正确的组中(即:美国场馆现在属于美国国家) 但现在发生的事情是,场馆并不是按名称排列的 我得到一张这样的清单 截图: 场馆现在按国家分组,但场馆本身不按A-Z顺序排列 我希望我的比赛场地安排在A-Z区,并且不会订购国家/地区
但我不知道该怎么做。我觉得你的截图不错。您可以按国家名称分组,并在分区内按场馆名称排序。出什么问题了?场馆不在正确的国家/地区。IE:American Airlines Arena在英国不存在,应该在美国而不是英国下列出。记录没有显示在正确的组中。哦,好的,我会试试这个。我现在已经解决了这个问题,我可以对场馆进行分组;但是这些场馆没有被订购。祝你成功!!你应该接受答案。至于订购,您可以在使用SortDescriptors:检索完物品后订购物品。我将对此进行拍摄。这很奇怪,因为我希望
场馆fetchAllGroupedBy:@“country.name”带有谓词:nil sortedBy:@“name”升序:是
按国家名称分组并按场馆名称排序;但看起来它并没有做到这一点;我将尝试排序描述符
[country setVenues:[NSSet setWithArray:listOfVenues]];
self.fetchedResultsController = [Venue fetchAllGroupedBy:@"country.name" withPredicate:nil sortedBy:@"country.name" ascending:YES delegate:self inContext:managedObjectContext];