Saya tahu mungkin ada solusi yang sangat sederhana untuk ini. Saya menggunakan contoh apple iphoneCoreDataRecipes. Saya mencoba menelusuri dari tabel awal (RecipeTypeTableViewController) dari RecipeTypes (yaitu Minuman, Makanan Penutup, Hidangan Utama) ke tampilan tabel berikutnya dengan daftar jenis Resep tersebut (RecipeListTableViewController). Saya membuat kelas baru RecipeType, berpikir saya bisa menggunakan ini untuk mengembalikan resep dari RecipeType yang dipilih. Sejauh ini hal terbaik yang bisa saya dapatkan adalah mengizinkan pemilihan tetapi menampilkan daftar semua resep yang sama. Kode asli dimulai dengan RecipeListTableViewController menggunakan NSFetchedResults untuk menampilkan semua resep. Sekarang saya memiliki tampilan awal RecipeTypes tetapi saya tidak yakin bagaimana cara meneruskan tipe yang dipilih ke RecipeListTableViewController. Saya perlu mengubah RecipeListTableViewController untuk menampilkan daftar RecipeType yang dipilih tetapi saya tidak yakin bagaimana menerapkannya. Saya rasa pada dasarnya saya ingin meneruskan NSMutableArray dari hasil yang diambil ke tampilan tabel lain tetapi tidak yakin arah mana yang harus diambil. Dan saya tidak ingin mengacaukan tindakan kerja lainnya di RecipeListTableViewController, yaitu mencari dan mengedit resep. Berikut gambar tindakan tabel sejauh ini [RecipeTypeTableView][1] & [RecipeListTableview][2]. Terima kasih sebelumnya atas saran apa pun.
Menghapus ini
File: RecipeType.h
Berkas: RecipeType.m
Saya menggunakan tipe NSManagedObject *dari Kelas Resep NSManagedObject saya untuk membuat tampilan awal jenis resep (Pembuka, Bev, dll.) di RecipeTypeTableViewController -
File: Resep.h
@interface Recipe : NSManagedObject
@property (nonatomic, strong) NSManagedObject *type;
Berkas: RecipeTypeTableViewController.h
// RecipeTypeTableViewController.h
// Recipes
#import <UIKit/UIKit.h>
@interface RecipeTypeTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObject *type;
@end
File: RecipeTypeTableViewController.m - bagian persiapan untuk segue
#import "RecipeTypeTableViewController.h"
#import "Recipe.h"
#import "RecipeListTableViewController.h"
@interface RecipeTypeTableViewController () <NSFetchedResultsControllerDelegate>
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@implementation RecipeTypeTableViewController
static NSString *MyIdentifier = @"showRecipes";
- (void)viewDidLoad {
[super viewDidLoad];
// register this class for our cell to this table view under the specified identifier 'ARecipeType'
self.title = @"Category";
//[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ARecipeType"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangePreferredContentSize:)name:UIContentSizeCategoryDidChangeNotification object:nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.title = @"Category";
}
- (void)didChangePreferredContentSize:(NSNotification *)notification
{
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
}
#pragma mark - Table view data source
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//return [[self.recipeTypeFetchedResultsController sections] count];
// return 1;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of recipe types
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
//return self.recipeTypes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ARecipeType" forIndexPath:indexPath];
// Configure the cell
NSManagedObject *recipeType = [self.recipeTypes objectAtIndex:indexPath.row];
cell.textLabel.text = [recipeType valueForKey:@"name"];
return cell;
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (_fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RecipeType" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entity);
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, 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:@"Recipe"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
return _fetchedResultsController;
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showRecipes"]) {
NSLog(@"Setting RecipeType for the RecipeListTableViewController");
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Recipe *selectedType = [self.fetchedResultsController objectAtIndexPath:indexPath];
RecipeListTableViewController *recipeListViewController = segue.destinationViewController;
recipeListViewController.type = selectedType;
recipeListViewController.managedObjectContext = self.managedObjectContext;
}
}
File: RecipeListTableViewController.h - bagian NSPredicate dan cache FRC
#import <UIKit/UIKit.h>
#import "RecipeAddViewController.h"
@interface RecipeListTableViewController : UITableViewController <RecipeAddDelegate, NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObject *type;
Berkas: RecipeListTableViewController.m
#import "RecipeListTableViewController.h"
#import "RecipeDetailViewController.h"
#import "Recipe.h"
#import "RecipeTableViewCell.h"
#import "Recipe+Extensions.h"
#import "TypeSelectionViewController.h"
#import "IngredientDetailViewController.h"
#import "Ingredient.h"
#import "WhereViewController.h"
#import "FavoriteListTableViewController.h"
#import "RecipeTypeTableViewController.h"
#import "RecipeType.h"
@interface RecipeListTableViewController () <NSFetchedResultsControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating>
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSArray *filteredList;
@property (nonatomic, strong) NSFetchRequest *searchFetchRequest;
@property (nonatomic, strong) UISearchController *searchController;
typedef NS_ENUM(NSInteger, RecipesSearchScope)
{
searchScopeRecipe = 0,
searchScopeIngredients = 1
};
@end
@implementation RecipeListTableViewController
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController {
// Set up the fetched results controller if needed.
if (_fetchedResultsController == nil) {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[NSFetchedResultsController deleteCacheWithName:@"Recipe"];
// Create predicate
//if (self.type) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == %@", self.type];
[fetchRequest setPredicate:predicate];
//}
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, 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:@"Recipe"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
}
return _fetchedResultsController;
}
/
Ketika saya mengatur cache FRC ke "Resep". Itu crash. Ini menunjukkan petunjuk ini untuk melihat cache CoreData: KESALAHAN FATAL: Cache persisten dari informasi bagian tidak cocok dengan konfigurasi saat ini. Anda telah secara ilegal memutasi permintaan pengambilan NSFetchedResultsController, predikatnya, atau deskripsi pengurutannya...
Jika saya menyetel cache ke nil, atau menambahkan [NSFetchedResultsController deleteCacheWithName:@"Recipe"]; sebelum predikat ditetapkan, semuanya berjalan sesuai harapan. Saya memiliki cache ini di pengontrol tampilan awal dan pengontrol tampilan kedua. Mungkin itu masalahnya - saya hanya memerlukan cache di pengontrol tampilan awal?