简体   繁体   English

核心数据NSSet问题

[英]Core Data NSSet Issue

I auto-generated the cored data NSManagedObject class, and what I am trying to do is to display all phone numbers for a certain person. 我自动生成了核心数据NSManagedObject类,而我想做的是显示某个人的所有电话号码。 I am trying to display this in my table view class to populate the table. 我试图在我的表视图类中显示此表以填充表。

Person to Numbers is a one-to-many relationship, and the numbers are in the set which I did by the addNumbersObject method. 人与数字是一对多的关系,数字在我通过addNumbersObject方法所做的集合中。 I just do not understand how to fetch this in the fetchresultscontroller and display them in the table view. 我只是不明白如何在fetchresultscontroller中获取它并在表视图中显示它们。

Currently I am just fetching all people. 目前,我只是拿所有人。

Any ideas or suggestions? 有什么想法或建议吗?

Core Data Class: 核心数据类:

@class Number

@interface Person : NSManagedObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *numbers;

@end

@interface Person (CoreDataGeneratedAccessors)

- (void)addNumbersObject:(Number *)value;
- (void)removeNumbersObject:(Number *)value;
- (void)addNumbers:(NSSet *)values;
- (void)removeNumbers:(NSSet *)values;

@end

Table View Class: 表格检视类别:

- (NSFetchedResultsController *)fetchedResultsController 
{    
    if (_fetchedResultsController != nil)
        return _fetchedResultsController;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:_context];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];

    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [fetchRequest release];
    [theFetchedResultsController release];

    return _fetchedResultsController;      
}

Try using a predicate on your fetchRequest. 尝试在fetchRequest上使用谓词。

I am not sure from your question if you want to display all the phone numbers for all the people or how you want that formatted, but this is how to get all the numbers for one person. 从您的问题中我不确定您是否要显示所有人的所有电话号码或要如何设置其格式,但这是如何获取一个人的所有电话号码。

To get all the phone numbers for one specific person in one table view: 在一个表格视图中获取某个特定人的所有电话号码:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Number" inManagedObjectContext:_context];

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"person.name == %@", personName]]; //Assuming person.name is unique

Then sort that person's phone number like you want it. 然后根据需要排序该人的电话号码。

There are a lot of ways you might "display all phone numbers for a person" -- think specifically about what you want your table to contain and how it should be organized, and you might have an easier time finding the answer you're looking for. 您可以通过多种方式“显示一个人的所有电话号码”-仔细考虑表格要包含的内容以及表格的组织方式,您可能会更轻松地找到要查找的答案对于。

In the meantime, here's one approach: Use table sections to represent Persons, and rows within each for their associated Numbers. 同时,这是一种方法:使用表格部分代表“人物”,并使用每个表格中的行关联其“数字”。 You can do this like so: 您可以这样做:

  1. Set Number as the entity for your fetch request. 将Number设置为您的提取请求的实体。
  2. Set "person.name" as the sort key. 将“ person.name”设置为排序关键字。 (You might want to add a second sort descriptor so that Numbers for each person are in a sensible order.) (您可能想要添加第二个排序描述符,以便每个人的数字都按合理的顺序排列。)
  3. Also set "person.name" as the section name key path. 还将“ person.name”设置为部分名称的关键路径。
  4. Implement your table view data source's -tableView:titleForHeaderInSection: thusly: 因此,实现表视图数据源的-tableView:titleForHeaderInSection:

     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo name]; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM