Advertisement
Guest User

PatientTableViewController

a guest
Sep 1st, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "PatientsTableTableViewController.h"
  2.  
  3. //import app delegate to gain access to managed object
  4. #import "LevithAppDelegate.h"
  5. //so we know what information we are communicating with
  6. #import "AddPatientViewController.h"
  7.  
  8.  
  9. @interface PatientsTableTableViewController ()
  10.  
  11. @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
  12.  
  13. //fetch request retursn an array of objects (patients names) from within the data base
  14. @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
  15.  
  16.  
  17. @end
  18.  
  19. @implementation PatientsTableTableViewController
  20.  
  21. - (id)initWithStyle:(UITableViewStyle)style
  22. {
  23.     self = [super initWithStyle:style];
  24.     if (self) {
  25.         // Custom initialization
  26.     }
  27.     return self;
  28. }
  29.  
  30. - (NSManagedObjectContext *) managedObjectContext {
  31.    
  32.     //this code allows us to commuicate with the managed object context throughout the project via
  33.     //"self.managedObjectContext
  34.     return [(LevithAppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
  35.    
  36. }
  37.  
  38. //prepare for segue
  39. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  40.    
  41.     //first check whether the segue is equal to our transition name (modal: addPatient in the storyboard)
  42.     if([[segue identifier]isEqualToString:@"addPatient"])
  43.     {
  44.         //tell the segue where to go
  45.         UINavigationController *navigationController = segue.destinationViewController;
  46.        
  47.         //create refernce to view controller
  48.         //able to do this because we imported "AddPatientViewController.h"
  49.         AddPatientViewController *addPatientViewController = (AddPatientViewController*) navigationController.topViewController;
  50.        
  51.         //create the new object
  52.         Patient *addPatient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext: [self managedObjectContext]];
  53.        
  54.         addPatientViewController.addPatient = addPatient;
  55.     }
  56. }
  57.  
  58.  
  59. - (void)viewDidLoad
  60. {
  61.     [super viewDidLoad];
  62.    
  63.     //create error object and set value to nil
  64.     NSError *error = nil;
  65.     //check that fetch results are available
  66.     if(![[self fetchedResultsController]performFetch:&error]){
  67.         NSLog(@"Error: %@", error);
  68.         abort();//remove if launching app as this will terminate the app
  69.     }
  70.    
  71. }
  72.  
  73. - (void)didReceiveMemoryWarning
  74. {
  75.     [super didReceiveMemoryWarning];
  76.     // Dispose of any resources that can be recreated.
  77. }
  78.  
  79. #pragma mark - Table view data source
  80.  
  81. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  82. {
  83.     //refer to the fetchresults to find out how many sections
  84.     //sections property then stores the amount as array
  85.     //then counts
  86.     return [[self.fetchedResultsController sections] count];
  87. }
  88.  
  89. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  90. {
  91.    
  92.     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
  93.  
  94.     return [sectionInfo numberOfObjects];
  95.    
  96. }
  97.  
  98.  
  99. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  100. {
  101.     //declare name and details for cell
  102.     //always confrim the cell identifier is the same as listed in your code
  103.     static NSString *CellIdentifier = @"Cell";
  104.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
  105.    
  106.     //create access to details the cell will display from the core date
  107.     Patient *patient = [self.fetchedResultsController objectAtIndexPath:indexPath];
  108.    
  109.     //create the title and subtitle (detail text label) information
  110.     cell.textLabel.text = patient.patientLastName;
  111.     cell.detailTextLabel.text = patient.patientFirstName;
  112.    
  113.     return cell;
  114. }
  115.  
  116.  
  117.  
  118. #pragma mark - Fetched Results Controller Section
  119.  
  120. // method for fetching results
  121. - (NSFetchedResultsController*) fetchedResultsController{
  122.     //FIRST WE "ASK" IF THERE IS ALREADY A FETCHED RESULTS CONTROLLER
  123.     //IF SO WE RETURN IT
  124.     if (_fetchedResultsController != nil){
  125.         return _fetchedResultsController;
  126.     }
  127.     //if we don't have one we create it
  128.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
  129.    
  130.     //we then access the managed object context and create a shortcut name
  131.     NSManagedObjectContext *context = [self managedObjectContext];
  132.    
  133.     //next we create access to the entity which we have two of: Patient and Perscription
  134.     //this entity is obviously Patient
  135.     //we access the context using the shortcut created above "context"
  136.     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Patient" inManagedObjectContext:context];
  137.    
  138.     //set the fetch request to fetch from out entity
  139.     [fetchRequest setEntity:entity];
  140.    
  141.     //create a sort descriptor to present sorted results
  142.     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"patientLastName" ascending:YES];
  143.  
  144.  
  145.     //create temporary array to hold our sorted objects
  146.     NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
  147.  
  148.     //assign array to sort descriptor property of the fetch request
  149.     fetchRequest.sortDescriptors = sortDescriptors;
  150.    
  151.     //initialise fetch results with fetch request
  152.     //not fetching in sections so section is nil
  153.     //cache is small and local so also nil
  154.     _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
  155.    
  156.     //create a delegate for fetch results to allow it to be called by others to fetch results
  157.     _fetchedResultsController.delegate = self;
  158.    
  159.     return _fetchedResultsController;
  160. }
  161.  
  162.  
  163. #pragma mark - Fetched Results Controller Delegates
  164.  
  165.  
  166.  
  167. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement