使用tableview中两个(或三个)实体的数据

时间:2013-09-02 14:42:02

标签: xcode uitableview core-data entity-relationship nsfetchedresultscontroller

我有一个应用程序存储高尔夫球轮的信息。我有三个实体,我将数据保存到:Rounds,Courses和Tees。我以这种方式构建了架构,因为Rounds和Courses之间存在两个很多的关系,而一个Course可以有多个T恤(蓝色,白色,金色等)。

我有一个UITableview,我想显示每轮的结果。但是,我想显示来自Rounds Entity和Courses Entity的数据。理想情况下,我也希望显示该轮的Tee,但这不是优先考虑的事项。

我的问题是,如何使用FetchResultsController从三个实体获取数据并将其显示在UITableview的单个单元格中?

以下是我将数据保存到实体的方法:

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;

如何在tableview单元格中获取圆形的课程名称和T恤(这将是T恤颜色)?

谢谢!

1 个答案:

答案 0 :(得分:1)

只需遵循关系即可。如果您在轮次和课程之间建立了一对多的关系,则需要在rounds等课程上建立关系,并在course等轮次上建立反比关系。因此,如果您为实体生成了子类,则可以像这样访问课程名称:

NSString *courseName = round.course.name;