1

我从书中找到的教程中构建了一些代码。它可以工作,我能够在 tableView 中成功显示来自 CoreData 的数据。我现在想识别 fetchRequest 返回的数据/对象。我觉得自己像个傻瓜,因为我无法理解足够的语法来隔离包含我的数据数组的对象。这是我难以理解的代码片段:

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

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sessions" inManagedObjectContext:_context];
    [fetchRequest setEntity:entity];
    //NSLog(@"Entity is set to: %@", entity);
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"refid" 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;

    return _fetchedResultsController;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];

}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    Sessions *info = [_fetchedResultsController objectAtIndexPath:indexPath];

    //Format cell data ready to be displayed
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EE, dd LLL yyyy"];
    NSString *dateString = [dateFormat stringFromDate:info.date];

    NSNumber *dist1Nbr = info.dist1;
    int dist1Int = [dist1Nbr integerValue];
    float distIntKorM = ([dist1Nbr integerValue])/1000;
    NSString *dist1StrMeters = [[NSString alloc] initWithFormat:@"%i", dist1Int];
    NSString *dist1StrKorM = [[NSString alloc] initWithFormat:@"%.01f", distIntKorM];


    //Select image to display
    if ([info.sport isEqualToString:@"Run"]) {
        UIImage *image = [UIImage imageNamed:@"trainers-15x10.png"];
        cell.imageView.image = image;
        cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport];
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@,  Dist: %@", info.sessiontype, dist1StrKorM];
    } else if ([info.sport isEqualToString:@"Other"]) {
        UIImage *image = [UIImage imageNamed:@"weights-15x10.png"];
        cell.imageView.image = image;
        cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@: (%@),", dateString, info.sport];
        cell.detailTextLabel.text = [[NSString alloc] initWithFormat:@"Type: %@,  Dist: %@", info.sessiontype, dist1StrKorM];
    } 

}

如果有人可以帮助我,将不胜感激。

4

2 回答 2

2

从您发布的代码中,NSFetchedresultsController 将返回按“refid”键降序排列的 Sessions 类的所有对象。NSFetchedResultsCONtroller 与 UITableViewController 一起使用,以向核心数据存储显示获取请求的结果。

但是,如果您只想在不使用表视图的情况下获取数组中的所有对象,您可以执行以下操作,例如在 viewWillAppear 中:

if (self.managedObjectContext) {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Sessions"];
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"refid" ascending:NO];
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    [self.fetchedResultsController performfetch];
    NSArray *results = [self.fetchedResultsController fetchedObjects]; // an array of Sessions objects ordered descending by refid key
    for (Sessions *sessions in results) {
        NSLog(@"Sessions = %@", sessions);
    }
}

您必须确保 NSManagedContext 之前已正确初始化,否则不会显示任何内容。

于 2013-08-26T20:20:30.087 回答
2

NSFetchedResultsSectionInfo您可以通过调用访问对象数组:

NSArray *sections = _fetchedResultsController.sections;

这些对象中的每一个都代表表中的一个部分。对于给定的部分,您可以执行以下操作:

id<NSFetchedResultsSectionInfo>section = sections[0];
NSArray *objects = section.objects;

访问该部分中的托管对象。或者,如果您有索引路径,则可以通过执行以下操作直接访问关联对象:

NSManagedObject *object = [_fetchedResultsController objectAtIndexPath:indexPath];

反向开启:

NSIndexPath *indexPath = [_fetchedResultsController indexPathForObject:object];

这是你要找的吗?

更新

忘记了这个。正如 Juan 下面建议的那样,您可以通过以下方式访问所有获取的对象:

NSArray *allObjects = _fetchedResultsController.fetchedObjects;
于 2013-08-26T20:08:12.900 回答