Kind of following this tutorial here: http://mobile.tutsplus.com/tutorials/iphone/iphone-core-data/
I use the fetch records method in my AppNameDelegate.m file. I then call reload on my tableview. Unlike the linked tutorial, I am not using a UITableViewController. Instead, I have added a table view to the NavigationController, and connected it to the IBOutlet variable called contactsTable.
Here’s my code for calling and loading the data:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:[self.navigationController view]];
[self fetchRecords];
[self.contactsTable reloadData];
[self.window makeKeyAndVisible];
}
The header file:
//
// SimpleContactsAppDelegate.h
// SimpleContacts
//
// Created by Aaron McLeod on 11-05-28.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface SimpleContactsAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
UINavigationController *navigationController;
// view for adding new contacts
UIViewController *newContactView;
UIButton *addButton;
// controls for the addContactView
UIButton *saveContactButton;
UITextField *nameField;
UITextField *emailField;
UITextField *phoneField;
UITableView *contactsTable;
NSMutableArray *contactArray;
}
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIButton *addButton;
@property (nonatomic, retain) IBOutlet UITableView *contactsTable;
@property (nonatomic, retain) NSMutableArray *contactArray;
// controller and fields for the form
@property (nonatomic, retain) IBOutlet UIViewController *newContactView;
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *emailField;
@property (nonatomic, retain) IBOutlet UITextField *phoneField;
@property (nonatomic, retain) IBOutlet UIButton *saveContactButton;
- (NSString *)applicationDocumentsDirectory;
- (IBAction) saveContact;
- (IBAction) switchToAddContactView;
- (void)fetchRecords;
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
@end
You can find my project source code here as well: https://github.com/agmcleod/SimpleContacts/tree/parttwo In case i left out code you might need. Thanks.
The only thing
reloadDatadoes is tell theUITableViewto ask its data source for new data. This means it’ll callnumberOfSectionsInTableView:etc. Have you set up this relationship correctly?In your case, your
SimpleContactsAppDelegateinstance should be thedataSourceof your table view (do the connection in Interface Builder).Consider using
NSFetchedResultsControllerwhen using Core Data and UITableViews. This allows you to not load all the objects into memory at once, etc. Xcode’s sample code provides a good starting point for this (I think it’s the Navigation Controller based app with Core Data template).