Advertisement
Guest User

TableView Code

a guest
Feb 18th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //RELEVANT CODE FOR THE FOLLOWING QUESTION
  2. //http://stackoverflow.com/questions/28576089/ios-delete-row-from-tableview-without-deleting-core-data
  3.  
  4. #pragma mark - Table view data source
  5.  
  6. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  7. {
  8.     //refer to the fetchresults to find out how many sections
  9.     //sections property then stores the amount as array
  10.     //then counts
  11.     return [[self.fetchedResultsController sections] count];
  12. }
  13.  
  14. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  15. {
  16.     //asking our sections parameters fetched results controller the number of a particular index
  17.     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
  18.    
  19.     //return this sections information
  20.     return [sectionInfo numberOfObjects];
  21.    
  22. }
  23.  
  24.  
  25. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  26. {
  27.     //declare name and details for cell
  28.     //always confrim the cell identifier is the same as listed in your code
  29.     static NSString *CellIdentifier = @"Cell";
  30.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
  31.    
  32.     //create access to details the cell will display from the core date
  33.     Materials * mats = [self.fetchedResultsController objectAtIndexPath:indexPath];
  34.    
  35.     NSString * quantityUsed = [NSString stringWithFormat:@"Quantity Used: %@", mats.quantityUsed];
  36.    
  37.     cell.textLabel.text = mats.partDescription;
  38.     cell.detailTextLabel.text = quantityUsed;
  39.    
  40.     return cell;
  41. }
  42.  
  43. # pragma mark Delete TableView Row
  44.  
  45. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  46.     // Return YES if you want the specified item to be editable.
  47.     return YES;
  48. }
  49.  
  50. // Override to support editing the table view.
  51. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  52.     if (editingStyle == UITableViewCellEditingStyleDelete) {
  53.        
  54.         //does not work
  55.     [myData removeAtIndex:indexPath.row]
  56.  
  57.     //does not work
  58.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
  59.  
  60.  
  61.     //DELETING A ROW **WILL** work IF I WANT TO DELETE AN OBJECT, WHICH I DO NOT WANT TO DO
  62.     NSManagedObjectContext * context = [self managedObjectContext];
  63.         Materials * rowToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
  64.         [context deleteObject: rowToDelete];
  65.        
  66.         NSError * error = nil;
  67.         if (![context save:&error]){
  68.             NSLog(@"Error: %@", error);
  69.         }
  70.  
  71.  
  72.  
  73.     }
  74.  
  75.     //a viable option to reload data in tableview
  76.     [self.tableView reloadData];
  77. }
  78.  
  79.  
  80. #pragma mark - Fetched Results Controller Delegate
  81.  
  82. //when fetched results are updated, the table should update
  83. - (void) controllerWillChangeContent:(NSFetchedResultsController *)controller{
  84.     [self.tableView beginUpdates];
  85. }
  86.  
  87. - (void) controllerDidChangeContent:(NSFetchedResultsController *)controller{
  88.     [self.tableView endUpdates];
  89. }
  90.  
  91. //handle the editing of a tableview
  92. - (void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
  93.    
  94.     //UITableView *tableView = self.safetyStepsTableView; //create a reference to the tableview
  95.    
  96.     switch (type) {
  97.         case NSFetchedResultsChangeInsert:
  98.             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  99.             break;
  100.         case NSFetchedResultsChangeDelete:
  101.             [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  102.             break;
  103.            
  104.         case NSFetchedResultsChangeUpdate:{
  105.             Materials * mats = [self.fetchedResultsController objectAtIndexPath:indexPath];
  106.             UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  107.             cell.textLabel.text = mats.partDescription;
  108.         }
  109.             break;
  110.            
  111.         case NSFetchedResultsChangeMove:
  112.             [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  113.             [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  114.             break;
  115.            
  116.     }
  117.    
  118.    
  119.    
  120. }
  121.  
  122. -(void) controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
  123.    
  124.     switch (type) {
  125.         case NSFetchedResultsChangeInsert:
  126.             [self.tableView insertSections:[NSIndexSet indexSetWithIndex: sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  127.             break;
  128.            
  129.         case NSFetchedResultsChangeDelete:
  130.             [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  131.             break;
  132.            
  133.         case NSFetchedResultsChangeMove:
  134.             NSLog(@"A table item was moved");
  135.             break;
  136.         case NSFetchedResultsChangeUpdate:
  137.             NSLog(@"A table item was updated");
  138.             break;
  139.            
  140.     }
  141.    
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement