Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Set the title.
- self.title = @"Locations";
- // Set up the buttons.
- self.navigationItem.leftBarButtonItem = self.editButtonItem;
- eventsArray = [[NSMutableArray alloc] init];
- addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
- target:self action:@selector(addEvent)];
- addButton.enabled = NO;
- self.navigationItem.rightBarButtonItem = addButton;
- // Start the location manager.
- [[self locationManager] startUpdatingLocation];
- NSFetchRequest *request = [[NSFetchRequest alloc] init];
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
- [request setEntity:entity];
- NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
- NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
- [request setSortDescriptors:sortDescriptors];
- NSError *error = nil;
- controller = [[NSFetchedResultsController alloc]
- initWithFetchRequest:request
- managedObjectContext:managedObjectContext
- sectionNameKeyPath:nil
- cacheName:nil];
- controller.delegate = self;
- }
- - (void)addEvent {
- CLLocation *location = [locationManager location];
- if (!location) {
- return;
- }
- Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];
- CLLocationCoordinate2D coordinate = [location coordinate];
- [event setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
- [event setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
- [event setCreationDate:[NSDate date]];
- NSError *error = nil;
- if (![managedObjectContext save:&error]) {
- // Handle the error.
- }
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
- [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
- withRowAnimation:UITableViewRowAnimationFade];
- [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- self.eventsArray = nil;
- self.locationManager = nil;
- self.addButton = nil;
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- [super viewWillDisappear:animated];
- }
- - (void)viewDidDisappear:(BOOL)animated
- {
- [super viewDidDisappear:animated];
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- #warning Potentially incomplete method implementation.
- // Return the number of sections.
- return [[controller sections] count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- id <NSFetchedResultsSectionInfo> sectionInfo = [[controller sections] objectAtIndex:section];
- return [sectionInfo numberOfObjects];
- }
- - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
- // Configure the cell to show the book's title
- // A date formatter for the time stamp.
- static NSDateFormatter *dateFormatter = nil;
- if (dateFormatter == nil) {
- dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
- [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
- }
- // A number formatter for the latitude and longitude.
- static NSNumberFormatter *numberFormatter = nil;
- if (numberFormatter == nil) {
- numberFormatter = [[NSNumberFormatter alloc] init];
- [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
- [numberFormatter setMaximumFractionDigits:3];
- }
- Event *event = (Event *)[controller objectAtIndexPath:indexPath];
- cell.textLabel.text = [dateFormatter stringFromDate:[event creationDate]];
- NSString *string = [NSString stringWithFormat:@"%@, %@",
- [numberFormatter stringFromNumber:[event latitude]],
- [numberFormatter stringFromNumber:[event longitude]]];
- cell.detailTextLabel.text = string;
- }
- - (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)controllerWillChangeContent:(NSFetchedResultsController *)controller {
- // The fetch controller is about to start sending change notifications, so prepare the table view for updates.
- [self.tableView beginUpdates];
- }
- - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
- switch(type) {
- case NSFetchedResultsChangeInsert:
- [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeDelete:
- [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeUpdate:
- [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
- break;
- case NSFetchedResultsChangeMove:
- [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- }
- }
- - (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)controllerDidChangeContent:(NSFetchedResultsController *)controller {
- // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
- [self.tableView endUpdates];
- }
- // Override to support conditional editing of the table view.
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Return NO if you do not want the specified item to be editable.
- return YES;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement