我有一个 JSON 读取的国家和地点列表。它现在可以正确读取并且似乎存储了关系。
// 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;
}
这在屏幕上产生的内容如下;
我尝试重置应用程序及其数据无济于事。
截图(通过Imageshack)
如您所见,该列表仅按字母顺序排列,不正确,也没有在相关部分中拆分。
似乎源于
Venue *v = [self.fetchedResultsController objectAtIndexPath:indexPath];
线,但我不知道如何解决它。
我想知道我做错了什么以及如何修复它,以便它在正确的部分显示正确的数据。