Advertisement
RRK

Location Example Coredata

RRK
Oct 22nd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.  
  5.     // Set the title.
  6.     self.title = @"Locations";
  7.    
  8.     // Set up the buttons.
  9.     self.navigationItem.leftBarButtonItem = self.editButtonItem;
  10.    
  11.     eventsArray = [[NSMutableArray alloc] init];
  12.    
  13.     addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
  14.                                                               target:self action:@selector(addEvent)];
  15.     addButton.enabled = NO;
  16.    
  17.     self.navigationItem.rightBarButtonItem = addButton;
  18.    
  19.     // Start the location manager.
  20.     [[self locationManager] startUpdatingLocation];
  21.    
  22.     NSFetchRequest *request = [[NSFetchRequest alloc] init];
  23.     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
  24.     [request setEntity:entity];
  25.    
  26.     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
  27.     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
  28.     [request setSortDescriptors:sortDescriptors];
  29.    
  30.     NSError *error = nil;
  31.  
  32.    controller = [[NSFetchedResultsController alloc]
  33.                                               initWithFetchRequest:request
  34.                                               managedObjectContext:managedObjectContext
  35.                                               sectionNameKeyPath:nil
  36.                                               cacheName:nil];
  37.    
  38.     controller.delegate = self;  
  39. }
  40.  
  41. - (void)addEvent {
  42.    
  43.     CLLocation *location = [locationManager location];
  44.     if (!location) {
  45.         return;
  46.     }    
  47.    
  48.     Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
  49.    
  50.     CLLocationCoordinate2D coordinate = [location coordinate];
  51.     [event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
  52.     [event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
  53.     [event setCreationDate:[NSDate date]];
  54.    
  55.     NSError *error = nil;
  56.     if (![managedObjectContext save:&error]) {
  57.         // Handle the error.
  58.     }
  59.    
  60.     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  61.     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
  62.                           withRowAnimation:UITableViewRowAnimationFade];
  63.     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  64. }
  65.  
  66. - (void)viewDidUnload
  67. {
  68.     [super viewDidUnload];
  69.     self.eventsArray = nil;
  70.     self.locationManager = nil;
  71.     self.addButton = nil;
  72. }
  73.  
  74. - (void)viewWillAppear:(BOOL)animated
  75. {
  76.     [super viewWillAppear:animated];
  77. }
  78.  
  79. - (void)viewDidAppear:(BOOL)animated
  80. {
  81.     [super viewDidAppear:animated];
  82. }
  83.  
  84. - (void)viewWillDisappear:(BOOL)animated
  85. {
  86.     [super viewWillDisappear:animated];
  87. }
  88.  
  89. - (void)viewDidDisappear:(BOOL)animated
  90. {
  91.     [super viewDidDisappear:animated];
  92. }
  93.  
  94. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  95. {
  96.     // Return YES for supported orientations
  97.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  98. }
  99.  
  100. #pragma mark - Table view data source
  101.  
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  103. {
  104. #warning Potentially incomplete method implementation.
  105.     // Return the number of sections.
  106.     return [[controller sections] count];
  107. }
  108.  
  109. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  110. {  
  111.     id <NSFetchedResultsSectionInfo> sectionInfo = [[controller sections] objectAtIndex:section];
  112.     return [sectionInfo numberOfObjects];
  113. }
  114.  
  115.  
  116. - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
  117.    
  118.     // Configure the cell to show the book's title
  119.     // A date formatter for the time stamp.
  120.     static NSDateFormatter *dateFormatter = nil;
  121.     if (dateFormatter == nil) {
  122.         dateFormatter = [[NSDateFormatter alloc] init];
  123.         [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
  124.         [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
  125.     }
  126.    
  127.     // A number formatter for the latitude and longitude.
  128.     static NSNumberFormatter *numberFormatter = nil;
  129.     if (numberFormatter == nil) {
  130.         numberFormatter = [[NSNumberFormatter alloc] init];
  131.         [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
  132.         [numberFormatter setMaximumFractionDigits:3];
  133.     }
  134.    
  135.     Event *event = (Event *)[controller objectAtIndexPath:indexPath];
  136.    
  137.     cell.textLabel.text = [dateFormatter stringFromDate:[event creationDate]];
  138.    
  139.     NSString *string = [NSString stringWithFormat:@"%@, %@",
  140.                         [numberFormatter stringFromNumber:[event latitude]],
  141.                         [numberFormatter stringFromNumber:[event longitude]]];
  142.     cell.detailTextLabel.text = string;
  143. }
  144.  
  145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {      
  147.     static NSString *CellIdentifier = @"Cell";
  148.    
  149.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  150.     if (cell == nil) {
  151.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  152.     }
  153.    
  154.     // Configure the cell...
  155.     [self configureCell:cell atIndexPath:indexPath];
  156.    
  157.     return cell;
  158. }
  159.  
  160.  
  161. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
  162.     // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
  163.     [self.tableView beginUpdates];
  164. }
  165.  
  166.  
  167. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
  168.    
  169.        
  170.     switch(type) {
  171.            
  172.         case NSFetchedResultsChangeInsert:
  173.             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  174.             break;
  175.            
  176.         case NSFetchedResultsChangeDelete:
  177.             [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  178.             break;
  179.            
  180.         case NSFetchedResultsChangeUpdate:
  181.             [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
  182.             break;
  183.            
  184.         case NSFetchedResultsChangeMove:
  185.             [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  186.             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  187.             break;
  188.     }
  189. }
  190.  
  191.  
  192. - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
  193.    
  194.     switch(type) {
  195.            
  196.         case NSFetchedResultsChangeInsert:
  197.             [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  198.             break;
  199.            
  200.         case NSFetchedResultsChangeDelete:
  201.             [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  202.             break;
  203.     }
  204. }
  205.  
  206.  
  207.  
  208. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
  209.     // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
  210.     [self.tableView endUpdates];
  211. }
  212.  
  213.  
  214. // Override to support conditional editing of the table view.
  215. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  216. {
  217.     // Return NO if you do not want the specified item to be editable.
  218.     return YES;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement