Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "PatientsTableTableViewController.h"
- //import app delegate to gain access to managed object
- #import "LevithAppDelegate.h"
- //so we know what information we are communicating with
- #import "AddPatientViewController.h"
- @interface PatientsTableTableViewController ()
- @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
- //fetch request retursn an array of objects (patients names) from within the data base
- @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- @end
- @implementation PatientsTableTableViewController
- - (id)initWithStyle:(UITableViewStyle)style
- {
- self = [super initWithStyle:style];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (NSManagedObjectContext *) managedObjectContext {
- //this code allows us to commuicate with the managed object context throughout the project via
- //"self.managedObjectContext
- return [(LevithAppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
- }
- //prepare for segue
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
- //first check whether the segue is equal to our transition name (modal: addPatient in the storyboard)
- if([[segue identifier]isEqualToString:@"addPatient"])
- {
- //tell the segue where to go
- UINavigationController *navigationController = segue.destinationViewController;
- //create refernce to view controller
- //able to do this because we imported "AddPatientViewController.h"
- AddPatientViewController *addPatientViewController = (AddPatientViewController*) navigationController.topViewController;
- //create the new object
- Patient *addPatient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext: [self managedObjectContext]];
- addPatientViewController.addPatient = addPatient;
- }
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //create error object and set value to nil
- NSError *error = nil;
- //check that fetch results are available
- if(![[self fetchedResultsController]performFetch:&error]){
- NSLog(@"Error: %@", error);
- abort();//remove if launching app as this will terminate the app
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- //refer to the fetchresults to find out how many sections
- //sections property then stores the amount as array
- //then counts
- return [[self.fetchedResultsController sections] count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
- return [sectionInfo numberOfObjects];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //declare name and details for cell
- //always confrim the cell identifier is the same as listed in your code
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
- //create access to details the cell will display from the core date
- Patient *patient = [self.fetchedResultsController objectAtIndexPath:indexPath];
- //create the title and subtitle (detail text label) information
- cell.textLabel.text = patient.patientLastName;
- cell.detailTextLabel.text = patient.patientFirstName;
- return cell;
- }
- #pragma mark - Fetched Results Controller Section
- // method for fetching results
- - (NSFetchedResultsController*) fetchedResultsController{
- //FIRST WE "ASK" IF THERE IS ALREADY A FETCHED RESULTS CONTROLLER
- //IF SO WE RETURN IT
- if (_fetchedResultsController != nil){
- return _fetchedResultsController;
- }
- //if we don't have one we create it
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
- //we then access the managed object context and create a shortcut name
- NSManagedObjectContext *context = [self managedObjectContext];
- //next we create access to the entity which we have two of: Patient and Perscription
- //this entity is obviously Patient
- //we access the context using the shortcut created above "context"
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"Patient" inManagedObjectContext:context];
- //set the fetch request to fetch from out entity
- [fetchRequest setEntity:entity];
- //create a sort descriptor to present sorted results
- NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patientLastName" ascending:YES];
- //create temporary array to hold our sorted objects
- NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
- //assign array to sort descriptor property of the fetch request
- fetchRequest.sortDescriptors = sortDescriptors;
- //initialise fetch results with fetch request
- //not fetching in sections so section is nil
- //cache is small and local so also nil
- _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
- //create a delegate for fetch results to allow it to be called by others to fetch results
- _fetchedResultsController.delegate = self;
- return _fetchedResultsController;
- }
- #pragma mark - Fetched Results Controller Delegates
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement