我一直在製作一個使用核心數據的程序。它有兩個實體,醫藥和日誌 藥品有一套日誌檢索關係coredata中的值
我有它添加一個日誌藥物時,它的第一次加入。這個日誌包含一個日期。 我想顯示藥物的最新日期(可以有多個日誌),但不知道如何檢索相應的日誌。我發現的教程只顯示如何添加但不能檢索。
這是我到目前爲止,它顯示藥物名稱。我想改變detailtextlabel展示最新的日誌條目
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"%@",object);
cell.textLabel.text = [object valueForKey:@"name"];
cell.detailTextLabel.text = [[object valueForKey:@"active"] stringValue];
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil) {
return __fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *medicines = [NSEntityDescription entityForName:@"Medicines" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:medicines];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __fetchedResultsController;
}
你能否提供一些關於你的* fetchedResultsController *的細節? –
目前它只能獲得藥品。我讀過一些頁面,說我需要以同樣的方式獲得第二張表格,其他人只是說我可以使用nsset日誌 –