У меня есть приложение, которое хранит информацию для раундов гольфа. У меня есть три сущности, которые я сохраняю данные: раунды, курсы и тройники. Я структурировал схему таким образом, потому что между раундами и курсами существует двух-много отношений, и курс может иметь несколько тройников (синий, белый, золотой и т. Д.)Использовать данные из двух (или трех) объектов в табличном представлении
У меня есть UITableview, что я хотел бы отображать результаты каждого раунда. Однако я хотел бы отображать данные из Rounds Entity, а также для Courses Entity. В идеале я также хотел бы показать Tee для этого раунда, но это не приоритет.
Мой вопрос: как я могу использовать FetchResultsController для получения данных из трех объектов и отображения их в одной ячейке UITableview?
Вот как я храню данные в Entities:
HandicapAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext* context = [appDelegate managedObjectContext];
NSEntityDescription *roundsEntity = [NSEntityDescription entityForName:@"Rounds" inManagedObjectContext:context];
NSFetchRequest *request =[[NSFetchRequest alloc]init];
[request setEntity:roundsEntity];
Rounds * rounds = [NSEntityDescription insertNewObjectForEntityForName:@"Rounds" inManagedObjectContext:context];
[rounds setValue:score forKey:@"roundScore"];
[rounds setValue:date forKey:@"roundDate"];
[rounds setValue:differential forKey:@"roundDifferential"];
Courses * courses = [NSEntityDescription insertNewObjectForEntityForName:@"Courses" inManagedObjectContext:context];
[courses setValue:rating forKey:@"courseRating"];
[courses setValue:slope forKey:@"courseSlope"];
[courses setValue:courseName forKey:@"courseName"];
rounds.courses = courses;
Tee * tee = [NSEntityDescription insertNewObjectForEntityForName:@"Tee" inManagedObjectContext:context];
[tee setValue:teeColor forKey:@"teeColor"];
courses.tees = tee;
NSError *error;
[context save:&error];
И тогда это мой FetchedResultsController
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"Rounds" inManagedObjectContext:self.managedObjectContext];
// Set the batch size
[fetchRequest setFetchBatchSize:20];
// Set the sort descriptor
NSSortDescriptor * date = [[NSSortDescriptor alloc] initWithKey: @"roundDate"
ascending: NO];
NSArray * sortDescriptors = [NSArray arrayWithObjects: date, nil];
fetchRequest.sortDescriptors = sortDescriptors;
// Initialize fetched results controller - creates cache
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest: fetchRequest
managedObjectContext: self.managedObjectContext
sectionNameKeyPath: nil
cacheName: @"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
// handle errors
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
Как принести название курса и тройник (это будет tee color) для раунда в ячейке tableview?
Спасибо!
Это сработало отлично. Благодаря! – tnb121