简体   繁体   中英

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?) 在此处输入图片说明

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(self.searchController.active) {
            NSMutableArray *theResult = [self.searchResult mutableCopy];

            [theResult removeObjectAtIndex:inIndexPath.row];
            self.searchResult = theResult;
            [inTableView deleteRowsAtIndexPaths:@[inIndexPath]
                               withRowAnimation:UITableViewRowAnimationFade];
            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];
        break;

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

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

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

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

You are using a single array for deleting object. You should check condition for deleting object. Does it is from search bar or normal. If it is from search bar then get that object and search it in main table view array and then delete it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM