我從書中找到的教程構建了一些代碼。它的工作原理,我能夠成功地顯示我的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];
}
}
如果有人可以幫助我,那將是大規模的讚賞。
你不明白? – tdelepine