Delete cell from UITableView not from Array

I learn how work with CoreData and Table view controller. I learn from a book and there is using this function for display content of TableViewControoler.

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(debug == 1){
        NSLog(@"Running %@ '%@'",self.class, NSStringFromSelector(_cmd));
    }

    static NSString *cellIndetifier = @"Class Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier
                                                            forIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    SchoolClass *class = [self.frc objectAtIndexPath:indexPath];
    NSMutableString *title = [NSMutableString stringWithFormat:@"%@", class.name];

    [title replaceOccurrencesOfString:@"(null)"
                           withString:@""
                              options:0
                                range:NSMakeRange(0, [title length])];

    cell.textLabel.text = title;

    return cell;
}

Now I would like delete a record(cell) from table view controller and from coreData. I don't know what is the simplest way, but I found this:

I have one button "delete" and the button has this IBA action.

-(IBAction)deleteClassAction:(id)sender{
    [self.tableView setEditing:YES animated:YES];
}

When I press the button I see something like this: http://i.stack.imgur.com/3VAyp.png

Now I need delete the selected cell. I found some code and I prepared skeleton for deleting. But I need advice in two things.

  1. I don't know how get the ID of deleting item and how to write code for filter - viz. comment: // the code that filters the object that has been marked for deleting cell from tableView

  2. I don't know how delete the cell from tableView. I found the solution, which is under the comment, but it has a error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (10) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Here is the code with comments.

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    // deleting data from CoreData database

    CoreDataHelper *cdh = [(AppDelegate *) [[UIApplication sharedApplication] delegate] cdh];

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];

    NSPredicate *filter = // the code that filters the object that has been marked for deleting cell from tableView"

    [request setPredicate:filter];

    NSArray *fetchedObjects = [cdh.context executeFetchRequest:request error:nil];

    for(SchoolClass *class in fetchedObjects){

        [_coreDataHelper.context deleteObject:class];
    }

    // deleting cell from tableView

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }


    [self.tableView reloadData];
}

Answers


You just need to delete the object from your NSFetchedResultsController. You might also then want to save the NSManagedObjectContext. The fetched results controller will then update the table view assuming you've implemented the delegate methods as described in the docs.

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

  // deleting cell from fetched results contoller

  if (editingStyle == UITableViewCellEditingStyleDelete) {

      SchoolClass *class = [self.frc objectAtIndexPath:indexPath];
      [[class managedObjectContext] deleteObject:class];
  }
}

Need Your Help

Django query is returning blank values

django postgresql python-2.7 django-models django-views

I'm writing an app that should list all teams from a local sports league inside a select box from which users can choose their favourite, however when I open the app everything looks good except th...