本で見つけたチュートリアルからいくつかのコードを作成しました。それは機能し、CoreData からのデータを tableView に正常に表示できます。ここで、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];
}
}
誰かが私を助けることができれば、それは大歓迎です。