Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //RELEVANT CODE FOR THE FOLLOWING QUESTION
- //http://stackoverflow.com/questions/28576089/ios-delete-row-from-tableview-without-deleting-core-data
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- //refer to the fetchresults to find out how many sections
- //sections property then stores the amount as array
- //then counts
- return [[self.fetchedResultsController sections] count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- //asking our sections parameters fetched results controller the number of a particular index
- id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
- //return this sections information
- return [sectionInfo numberOfObjects];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //declare name and details for cell
- //always confrim the cell identifier is the same as listed in your code
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier forIndexPath:indexPath];
- //create access to details the cell will display from the core date
- Materials * mats = [self.fetchedResultsController objectAtIndexPath:indexPath];
- NSString * quantityUsed = [NSString stringWithFormat:@"Quantity Used: %@", mats.quantityUsed];
- cell.textLabel.text = mats.partDescription;
- cell.detailTextLabel.text = quantityUsed;
- return cell;
- }
- # pragma mark Delete TableView Row
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- // Return YES if you want the specified item to be editable.
- return YES;
- }
- // Override to support editing the table view.
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- //does not work
- [myData removeAtIndex:indexPath.row]
- //does not work
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
- //DELETING A ROW **WILL** work IF I WANT TO DELETE AN OBJECT, WHICH I DO NOT WANT TO DO
- NSManagedObjectContext * context = [self managedObjectContext];
- Materials * rowToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
- [context deleteObject: rowToDelete];
- NSError * error = nil;
- if (![context save:&error]){
- NSLog(@"Error: %@", error);
- }
- }
- //a viable option to reload data in tableview
- [self.tableView reloadData];
- }
- #pragma mark - Fetched Results Controller Delegate
- //when fetched results are updated, the table should update
- - (void) controllerWillChangeContent:(NSFetchedResultsController *)controller{
- [self.tableView beginUpdates];
- }
- - (void) controllerDidChangeContent:(NSFetchedResultsController *)controller{
- [self.tableView endUpdates];
- }
- //handle the editing of a tableview
- - (void) controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
- //UITableView *tableView = self.safetyStepsTableView; //create a reference to the tableview
- 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:{
- Materials * mats = [self.fetchedResultsController objectAtIndexPath:indexPath];
- UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
- cell.textLabel.text = mats.partDescription;
- }
- 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;
- case NSFetchedResultsChangeMove:
- NSLog(@"A table item was moved");
- break;
- case NSFetchedResultsChangeUpdate:
- NSLog(@"A table item was updated");
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement