@interface ImageToDataTransformer : NSValueTransformer { } @end @interface Recipe : NSManagedObject { } @property (nonatomic, retain) NSString *instructions; @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSString *overview; @property (nonatomic, retain) NSString *prepTime; @property (nonatomic, retain) NSSet *ingredients; @property (nonatomic, retain) UIImage *thumbnailImage; @property (nonatomic, retain) NSManagedObject *image; @property (nonatomic, retain) NSManagedObject *type; @end @interface Recipe (CoreDataGeneratedAccessors) - (void)addIngredientsObject:(NSManagedObject *)value; - (void)removeIngredientsObject:(NSManagedObject *)value; - (void)addIngredients:(NSSet *)value; - (void)removeIngredients:(NSSet *)value; @end
@class Recipe; @interface Ingredient : NSManagedObject { } @property (nonatomic, retain) NSString *name; @property (nonatomic, retain) NSString *amount; @property (nonatomic, retain) Recipe *recipe; @property (nonatomic, retain) NSNumber *displayOrder; @end
@class RecipeListTableViewController; @interface RecipesAppDelegate : NSObject <UIApplicationDelegate> { NSManagedObjectModel *managedObjectModel; NSManagedObjectContext *managedObjectContext; NSPersistentStoreCoordinator *persistentStoreCoordinator; } @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; - (NSString *)applicationDocumentsDirectory; @end
... - (void)applicationDidFinishLaunching:(UIApplication *)application { recipeListController.managedObjectContext = self.managedObjectContext; ... } // , , - (void)applicationWillTerminate:(UIApplication *)application { NSError *error; if (managedObjectContext != nil) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } } // NSManagedObjectContext, - (NSManagedObjectContext *)managedObjectContext { if (managedObjectContext != nil) { return managedObjectContext; } NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; if (coordinator != nil) { managedObjectContext = [NSManagedObjectContext new]; [managedObjectContext setPersistentStoreCoordinator: coordinator]; } return managedObjectContext; } // NSManagedObjectModel, - (NSManagedObjectModel *)managedObjectModel { if (managedObjectModel != nil) { return managedObjectModel; } managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; return managedObjectModel; } // NSPersistentStoreCoordinator, . NSPersistentStoreCoordinator - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (persistentStoreCoordinator != nil) { return persistentStoreCoordinator; } NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Recipes.sqlite"]; /* , , . . */ NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:storePath]) { NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"sqlite"]; if (defaultStorePath) { [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; } } NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; NSError *error; persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return persistentStoreCoordinator; } // Documents - (NSString *)applicationDocumentsDirectory { return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; } ...
... // Recipe - (NSFetchedResultsController *)fetchedResultsController { if (fetchedResultsController == nil) { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; // NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; [aFetchedResultsController release]; [fetchRequest release]; [sortDescriptor release]; [sortDescriptors release]; } return fetchedResultsController; } ... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // RecipeTableViewCell . RecipeTableViewCell UITableViewCell static NSString *RecipeCellIdentifier = @"RecipeCellIdentifier"; RecipeTableViewCell *recipeCell = (RecipeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RecipeCellIdentifier]; if (recipeCell == nil) { recipeCell = [[[RecipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RecipeCellIdentifier] autorelease]; recipeCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } [self configureCell:recipeCell atIndexPath:indexPath]; return recipeCell; } - (void)configureCell:(RecipeTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { // Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath]; cell.recipe = recipe; } ...
Recipe *recipe; NSMutableArray *ingredients; @property (nonatomic, retain) Recipe *recipe; @property (nonatomic, retain) NSMutableArray *ingredients;
... // - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Recipe *recipe = (Recipe *)[fetchedResultsController objectAtIndexPath:indexPath]; [self showRecipe:recipe animated:YES]; } ... - (void)showRecipe:(Recipe *)recipe animated:(BOOL)animated { RecipeDetailViewController *detailViewController = [[RecipeDetailViewController alloc] initWithStyle:UITableViewStyleGrouped]; // detailViewController.recipe = recipe; [self.navigationController pushViewController:detailViewController animated:animated]; [detailViewController release]; } ...
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [photoButton setImage:recipe.thumbnailImage forState:UIControlStateNormal]; self.navigationItem.title = recipe.name; nameTextField.text = recipe.name; overviewTextField.text = recipe.overview; prepTimeTextField.text = recipe.prepTime; [self updatePhotoButton]; // NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"displayOrder" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; NSMutableArray *sortedIngredients = [[NSMutableArray alloc] initWithArray:[recipe.ingredients allObjects]]; [sortedIngredients sortUsingDescriptors:sortDescriptors]; self.ingredients = sortedIngredients; [sortDescriptor release]; [sortDescriptors release]; [sortedIngredients release]; [self.tableView reloadData]; }
static NSString *IngredientsCellIdentifier = @"IngredientsCell"; cell = [tableView dequeueReusableCellWithIdentifier:IngredientsCellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IngredientsCellIdentifier] autorelease]; cell.accessoryType = UITableViewCellAccessoryNone; }
Source: https://habr.com/ru/post/136319/
All Articles