RRK

FRC and TableViewDelegate

RRK
Nov 2nd, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I have one-to-many relationship between boss and employee, I am trying to reorder cells in my tableview, and this I am doing without entering the editMode, Please look at this function
  2.  
  3. - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
  4.  
  5. and to fecth boss items I have used FRC.And to fecth employee items I have not used FRC and the above function works properly there.
  6.  
  7.  
  8. #import "BossViewController.h"
  9. #import "AddItemsViewController.h"
  10. #import "EmployeeViewController.h"
  11. #import "Boss.h"
  12.  
  13. @implementation BossViewController
  14. @synthesize controller = m_controller;
  15.  
  16. @synthesize managedObjectContext = m_managedObjectContext;
  17.  
  18. - (void)viewDidLoad
  19. {  
  20.     [super viewDidLoad];  
  21.    
  22.     self.navigationItem.title = @"Boss";
  23.    
  24.     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddFolder:)];  
  25.     self.navigationItem.rightBarButtonItem = addButton;
  26.    
  27.    
  28.     //Adding as a subview.  
  29.     m_editViewController = [[AddItemsViewController alloc] initWithNibName:@"AddItemsViewController" bundle:nil];
  30.     m_editViewController.delegate = self;
  31.     [self.view addSubview:m_editViewController.view];
  32.     m_editViewController.view.hidden = YES;
  33.    
  34.   }
  35.  
  36. - (void)insertNewObject:(NSString *)fileName
  37. {    
  38.     Boss *bossName = [NSEntityDescription insertNewObjectForEntityForName:@"Boss" inManagedObjectContext:self.managedObjectContext];
  39.    
  40.     [bossName setName:fileName];
  41.    
  42.     // Save the context.
  43.     NSError *error = nil;
  44.     if (![self.managedObjectContext save:&error]) {
  45.         // Replace this implementation with code to handle the error appropriately.
  46.         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  47.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  48.         abort();
  49.     }
  50. }
  51.  
  52. - (NSFetchedResultsController *)controller
  53. {
  54.     if (m_controller != nil) {
  55.         return m_controller;
  56.     }
  57.    
  58.     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  59.     // Edit the entity name as appropriate.
  60.     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Boss" inManagedObjectContext:self.managedObjectContext];
  61.     [fetchRequest setEntity:entity];
  62.    
  63.     // Set the batch size to a suitable number.
  64.     [fetchRequest setFetchBatchSize:20];
  65.    
  66.     // Edit the sort key as appropriate.
  67.     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
  68.     NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
  69.    
  70.     [fetchRequest setSortDescriptors:sortDescriptors];
  71.    
  72.     // Edit the section name key path and cache name if appropriate.
  73.     // nil for section name key path means "no sections".
  74.     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
  75.     aFetchedResultsController.delegate = self;
  76.     self.controller = aFetchedResultsController;
  77.    
  78.     NSError *error = nil;
  79.     if (![self.controller performFetch:&error]) {
  80.         // Replace this implementation with code to handle the error appropriately.
  81.         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
  82.         NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
  83.         abort();
  84.     }
  85.    
  86.     return  m_controller;
  87. }
  88.  
  89.  
  90. #pragma mark - Table view data source
  91.  
  92. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  93. {
  94.     // Return the number of sections.
  95.     return [[self.controller sections] count];
  96. }
  97.  
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  99. {
  100.     // Return the number of rows in the section.        
  101.     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
  102.     return [sectionInfo numberOfObjects];      
  103.    
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107.     static NSString *CellIdentifier = @"Cell";      
  108.    
  109.     UITableViewCell  *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  110.    
  111.     if (cell == nil) {
  112.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  113.     }
  114.    
  115.     Boss *bossName = [self.controller objectAtIndexPath:indexPath];
  116.     cell.textLabel.text = bossName.name;  
  117.    
  118.     return cell;
  119. }
  120.  
  121. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
  122. {
  123.     [m_tableView beginUpdates];
  124. }
  125.  
  126. - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
  127.            atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
  128. {
  129.     switch(type) {
  130.         case NSFetchedResultsChangeInsert:
  131.             [m_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  132.             break;
  133.            
  134.         case NSFetchedResultsChangeDelete:
  135.             [m_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  136.             break;
  137.     }
  138. }
  139.  
  140. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
  141.        atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
  142.       newIndexPath:(NSIndexPath *)newIndexPath
  143. {
  144.     UITableView *tableView = m_tableView;
  145.    
  146.     switch(type) {
  147.         case NSFetchedResultsChangeInsert:
  148.             [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  149.             break;
  150.            
  151.         case NSFetchedResultsChangeDelete:
  152.             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  153.             break;
  154.            
  155.         case NSFetchedResultsChangeMove:
  156.             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  157.             [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
  158.             break;
  159.     }
  160. }
  161.  
  162. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
  163. {
  164.     [m_tableView endUpdates];
  165. }
  166.  
  167. // Override to support conditional rearranging of the table view.
  168. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170.     // Return NO if you do not want the item to be re-orderable.
  171.     return YES;
  172. }
  173.  
  174. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176.     return UITableViewCellEditingStyleNone;
  177. }
  178.  
  179. - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
  180. {
  181.     return NO;
  182. }
  183.  
  184. //Reorder control transformation
  185. - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  186. {
  187.     //  Grip customization code goes in here...
  188.    
  189.     for(UIView* view in cell.subviews)
  190.     {
  191.     //this condition is not getting executed
  192.         if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
  193.         {
  194.             // Creates a new subview the size of the entire cell
  195.             UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
  196.             // Adds the reorder control view to our new subview
  197.             [movedReorderControl addSubview:view];
  198.             // Adds our new subview to the cell
  199.             [cell addSubview:movedReorderControl];
  200.             // CGStuff to move it to the left
  201.             CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
  202.             CGAffineTransform transform = CGAffineTransformIdentity;
  203.             transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
  204.             // Performs the transform
  205.             [movedReorderControl setTransform:transform];
  206.            
  207.             for(UIImageView* cellGrip in view.subviews)
  208.             {              
  209.                 cellGrip.frame = CGRectMake(0, 0, 20, 20);
  210.                 if([cellGrip isKindOfClass:[UIImageView class]])  
  211.                    
  212.                     [cellGrip setImage:[UIImage imageNamed:@"square_but_drag_clicked.png"]];          
  213.             }
  214.         }
  215.     }    
  216. }
  217.  
  218. // Override to support rearranging the table view.
  219. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
  220. {  
  221.    
  222. }
  223.  
  224. @end
Advertisement
Add Comment
Please, Sign In to add comment