Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- 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.
- #import "BossViewController.h"
- #import "AddItemsViewController.h"
- #import "EmployeeViewController.h"
- #import "Boss.h"
- @implementation BossViewController
- @synthesize controller = m_controller;
- @synthesize managedObjectContext = m_managedObjectContext;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.navigationItem.title = @"Boss";
- UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(AddFolder:)];
- self.navigationItem.rightBarButtonItem = addButton;
- //Adding as a subview.
- m_editViewController = [[AddItemsViewController alloc] initWithNibName:@"AddItemsViewController" bundle:nil];
- m_editViewController.delegate = self;
- [self.view addSubview:m_editViewController.view];
- m_editViewController.view.hidden = YES;
- }
- - (void)insertNewObject:(NSString *)fileName
- {
- Boss *bossName = [NSEntityDescription insertNewObjectForEntityForName:@"Boss" inManagedObjectContext:self.managedObjectContext];
- [bossName setName:fileName];
- // Save the context.
- NSError *error = nil;
- if (![self.managedObjectContext save:&error]) {
- // Replace this implementation with code to handle the error appropriately.
- // 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.
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- }
- - (NSFetchedResultsController *)controller
- {
- if (m_controller != nil) {
- return m_controller;
- }
- NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
- // Edit the entity name as appropriate.
- NSEntityDescription *entity = [NSEntityDescription entityForName:@"Boss" inManagedObjectContext:self.managedObjectContext];
- [fetchRequest setEntity:entity];
- // Set the batch size to a suitable number.
- [fetchRequest setFetchBatchSize:20];
- // Edit the sort key as appropriate.
- NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
- NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
- [fetchRequest setSortDescriptors:sortDescriptors];
- // Edit the section name key path and cache name if appropriate.
- // nil for section name key path means "no sections".
- NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
- aFetchedResultsController.delegate = self;
- self.controller = aFetchedResultsController;
- NSError *error = nil;
- if (![self.controller performFetch:&error]) {
- // Replace this implementation with code to handle the error appropriately.
- // 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.
- NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
- abort();
- }
- return m_controller;
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- // Return the number of sections.
- return [[self.controller sections] count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- id <NSFetchedResultsSectionInfo> sectionInfo = [[self.controller sections] objectAtIndex:section];
- return [sectionInfo numberOfObjects];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- }
- Boss *bossName = [self.controller objectAtIndexPath:indexPath];
- cell.textLabel.text = bossName.name;
- return cell;
- }
- - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
- {
- [m_tableView beginUpdates];
- }
- - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
- atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
- {
- switch(type) {
- case NSFetchedResultsChangeInsert:
- [m_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeDelete:
- [m_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
- break;
- }
- }
- - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
- atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
- newIndexPath:(NSIndexPath *)newIndexPath
- {
- UITableView *tableView = m_tableView;
- switch(type) {
- case NSFetchedResultsChangeInsert:
- [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeDelete:
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeMove:
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
- break;
- }
- }
- - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
- {
- [m_tableView endUpdates];
- }
- // Override to support conditional rearranging of the table view.
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Return NO if you do not want the item to be re-orderable.
- return YES;
- }
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleNone;
- }
- - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return NO;
- }
- //Reorder control transformation
- - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- // Grip customization code goes in here...
- for(UIView* view in cell.subviews)
- {
- //this condition is not getting executed
- if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"])
- {
- // Creates a new subview the size of the entire cell
- UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
- // Adds the reorder control view to our new subview
- [movedReorderControl addSubview:view];
- // Adds our new subview to the cell
- [cell addSubview:movedReorderControl];
- // CGStuff to move it to the left
- CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - view.frame.size.width, movedReorderControl.frame.size.height - view.frame.size.height);
- CGAffineTransform transform = CGAffineTransformIdentity;
- transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
- // Performs the transform
- [movedReorderControl setTransform:transform];
- for(UIImageView* cellGrip in view.subviews)
- {
- cellGrip.frame = CGRectMake(0, 0, 20, 20);
- if([cellGrip isKindOfClass:[UIImageView class]])
- [cellGrip setImage:[UIImage imageNamed:@"square_but_drag_clicked.png"]];
- }
- }
- }
- }
- // Override to support rearranging the table view.
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
- {
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment