2013-08-26 184 views
1

我從書中找到的教程構建了一些代碼。它的工作原理,我能夠成功地顯示我的CoreData數據在一個tableView。我現在想識別fetchRequest返回的數據/對象。我覺得這樣的假人是因爲我無法理解足夠的語法來隔離包含我的數據數組的對象。這是我難以理解的代碼片段:確定NSFetchedResultsController中的數組數組

- (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]; 
    } 

} 

如果有人可以幫助我,那將是大規模的讚賞。

+0

你不明白? – tdelepine

回答

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]; 

這是你在找什麼?

更新

忘記了這一點。胡安以下建議,您可以訪問所有獲取的對象有:

NSArray *allObjects = _fetchedResultsController.fetchedObjects; 
2

從您發佈的NSFetchedresultsController是否會返回一個班會議的所有對象下令鍵降碼「REFID」。 NSFetchedResultsCOntroller與UITableViewController一起使用,以向Core Data存儲顯示提取請求的結果。

不過,如果你只想獲得數組中的所有對象,而不使用表格視圖,你可以做到以下幾點,例如在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是正確的初始化之前或沒有什麼會顯示。