mercredi 24 juin 2015

Deleting a cell in a tableview while searchbar is active - CoreData Error

I am pretty new to IOS Programming and this will be my first app. I have a TableViewController to show the Data History (CoreData) and I am trying to implement a SearchBar (iOS8). Besides a minor issure with the layout it works fine (see Screenshot, the tableView does not have full width and there is this lupe...?, does anyone know why this is?) enter image description here

My bigger problem is that when I delete a row without using the searchbar it works, but when I am searching for an entry and try to delete, it deleted it but I get an exception and the view gets stuck with the red "Deletion"-Button. If I go back to the main view and back to the tableview the view is updated..

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582 2015-06-24 10:55:25.635 Pedal[9203:284856] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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). with userInfo (null)

Here's my code for the two methods I think are relevant (correct me if I am wrong)

- (void)tableView:(UITableView *)inTableView commitEditingStyle:(UITableViewCellEditingStyle)inStyle forRowAtIndexPath:(NSIndexPath *)inIndexPath {
if(inStyle == UITableViewCellEditingStyleDelete) {
    //[inTableView beginUpdates];

    RecordingInfo *theItem = [self entryAtIndexPath:inIndexPath]; 
    NSError *theError = nil;

    [self.managedObjectContext deleteObject:theItem];
    if([self.managedObjectContext save:&theError]) {

        if( {
            NSMutableArray *theResult = [self.searchResult mutableCopy];

            [theResult removeObjectAtIndex:inIndexPath.row];
            self.searchResult = theResult;
            [inTableView deleteRowsAtIndexPaths:@[inIndexPath]
            NSLog(@"deletion successful");
    else {
        NSLog(@"Unresolved error while deleting %@", theError);
    //[self.tableView endUpdates];

is it possible I need to add a similar if-question in controller:didChangeObject:?

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

UITableView *tableView = self.tableView;

switch(type) {

    case NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray
                                           arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray
                                           arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

If anyone can help I'd be very glad, I am trying to solve this forever now! Thanks!

See more ..

Aucun commentaire:

Enregistrer un commentaire