在使用核心数据进行培训时,我多次遇到问题,当我按下导航栏中的添加按钮时,它会推送一个新的 ViewController,我可以在其中插入数据以创建新的对象。在我按下保存导航栏后,弹出视图控制器,我再次看到我的表格。
大多数情况下,表格不会显示元素,但是如果我单击出现元素名称的行,它看起来就像表格需要重新加载,因为元素在那里但没有显示。
为什么会这样?这是我的应用程序的代码:我复制了所有类,但我相信你可以跳过 appdelegate 和 masterviewcontroller。
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "MasterViewController.h"
#import "IngredientsTableViewController.h"
@implementation AppDelegate
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterVC = [[MasterViewController alloc]initWithNibName:@"MasterViewCOntroller" bundle:nil];
//masterVC.managedObjectContex = self.managedObjectContext;
IngredientsTableViewController *ingredientsTVC = [[IngredientsTableViewController alloc]initWithNibName:@"IngredientsTableViewController" bundle:nil];
ingredientsTVC.managedObjectContext = self.managedObjectContext;
UINavigationController *navigationControllerIngredients = [[UINavigationController alloc]initWithRootViewController:ingredientsTVC];
[masterVC setViewControllers:@[navigationControllerIngredients]];
[self.window setRootViewController:masterVC];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//to save space i didn't copy the autogenerated methods
主视图控制器.h
#import <UIKit/UIKit.h>
@class IngredientsTableViewController;
@interface MasterViewController : UITabBarController {
}
@end
主视图控制器.m
#import "MasterViewController.h"
#import "IngredientsTableViewController.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
@end
成分表视图控制器.h
import <UIKit/UIKit.h>
#import "DetailIngredientViewController.h"
#import "Ingredient.h"
@interface IngredientsTableViewController : UITableViewController <NSFetchedResultsControllerDelegate, DetailIngredientViewControllerDelegate> {
Ingredient *ingredient;
}
@property (nonatomic)NSManagedObjectContext *managedObjectContext;
@property (nonatomic)NSFetchedResultsController *fetchedResultsController;
@property (nonatomic)Ingredient *ingredient;
@end
成分表视图控制器.m
#import "IngredientsTableViewController.h"
@interface IngredientsTableViewController ()
@end
@implementation IngredientsTableViewController
@synthesize managedObjectContext = _managedObjectContext;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize ingredient = _ingredient;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Ingredients";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addIngredient)];
self.navigationItem.rightBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Action
-
(void)addIngredient{
DetailIngredientViewController *detailIngredientVC =[[DetailIngredientViewController alloc]initWithNibName:@"DetailIngredientViewController" bundle:nil];
detailIngredientVC.delegate = self;
self.ingredient = [NSEntityDescription insertNewObjectForEntityForName:@"Ingredient" inManagedObjectContext:self.managedObjectContext];
detailIngredientVC.detailIngredient = self.ingredient;
[self.navigationController pushViewController:detailIngredientVC animated:YES];
}
#pragma mark - Fetched Result
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
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;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
#pragma mark - configureCell:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[object valueForKey:@"name"] description];
}
-(void)controller:(UIViewController *)controller didSaveIngredient:(Ingredient *)DetailIngredient{
NSError *error = nil;
if (![self.managedObjectContext save:&error]){
NSLog(@"Error %@ %@", error, [error userInfo]);
}
}
@end
DetailIngredientViewController.h
#import <UIKit/UIKit.h>
#import "Ingredient.h"
@protocol DetailIngredientViewControllerDelegate;
@interface DetailIngredientViewController : UIViewController {
IBOutlet UITextField *tfNameIngredient;
id <DetailIngredientViewControllerDelegate>delegate;
Ingredient *detailIngredient;
}
@property (nonatomic, retain) IBOutlet UITextField *tfNameIngredient;
@property (nonatomic, retain) id <DetailIngredientViewControllerDelegate>delegate;
@property (nonatomic, retain) Ingredient *detailIngredient;
@end
@protocol DetailIngredientViewControllerDelegate <NSObject>
-(void)controller:(UIViewController *)controller didSaveIngredient:(Ingredient *)DetailIngredient;
@end
DetailIngredientViewController.m
#import "DetailIngredientViewController.h"
@interface DetailIngredientViewController ()
@end
@implementation DetailIngredientViewController
@synthesize tfNameIngredient, detailIngredient;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)saveIngredient{
detailIngredient.name = tfNameIngredient.text;
NSLog(@"1- %@", detailIngredient.name);
[self.delegate controller:self didSaveIngredient:detailIngredient];
[self.navigationController popToRootViewControllerAnimated:YES];
}
@end