0

i've got a problem by deleting cells in a section.The tableViewController has three sections with various cells. If I try to delete one cell, the debugger will show:

2015-01-23 20:22:15.105 Grade - Zensurenverwaltung[23854:5674475] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1566 2015-01-23 20:22:15.133 Grade - Zensurenverwaltung[23854:5674475] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (3) must be equal to the number of sections contained in the table view before the update (3), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).' *** First throw call stack: ( 0 CoreFoundation 0x00000001008bef35 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001025adbb7 objc_exception_throw + 45 2 CoreFoundation 0x00000001008bed9a +[NSException raise:format:arguments:] + 106 3 Foundation 0x0000000100d565df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x00000001013c98ff -[UITableView _endCellAnimationsWithContext:] + 10935 5 Grade - Zensurenverwaltung 0x00000001000fa0c2 _TFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 3618 6 Grade - Zensurenverwaltung 0x00000001000fa207 _TToFC26Grade___Zensurenverwaltung28TestTypesTableViewController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87 7 UIKit 0x00000001013edcb4 -[UITableView animateDeletionOfRowWithCell:] + 130 8 UIKit 0x00000001013ce125 __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72 9 UIKit 0x00000001012f68be -[UIApplication sendAction:to:from:forEvent:] + 75 10 UIKit 0x00000001013fd410 -[UIControl _sendActionsForEvents:withEvent:] + 467 11 UIKit 0x00000001013fc7df -[UIControl touchesEnded:withEvent:] + 522 12 UIKit 0x00000001016a3540 _UIGestureRecognizerUpdate + 9487 13 UIKit 0x000000010133bff6 -[UIWindow _sendGesturesForEvent:] + 1041 14 UIKit 0x000000010133cc23 -[UIWindow sendEvent:] + 667 15 UIKit 0x00000001013099b1 -[UIApplication sendEvent:] + 246 16 UIKit 0x0000000101316a7d _UIApplicationHandleEventFromQueueEvent + 17370 17 UIKit 0x00000001012f2103 _UIApplicationHandleEventQueue + 1961 18 CoreFoundation 0x00000001007f4551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 19 CoreFoundation 0x00000001007ea41d __CFRunLoopDoSources0 + 269 20 CoreFoundation 0x00000001007e9a54 __CFRunLoopRun + 868 21 CoreFoundation 0x00000001007e9486 CFRunLoopRunSpecific + 470 22 GraphicsServices 0x00000001052fd9f0 GSEventRunModal + 161 23 UIKit 0x00000001012f5420 UIApplicationMain + 1282 24 Grade - Zensurenverwaltung 0x00000001001e164e top_level_code + 78 25 Grade - Zensurenverwaltung 0x00000001001e168a main + 42 26 libdyld.dylib 0x0000000102d87145 start + 1 27 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

And i just don't get it...

Here is my code:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    var indexes: NSMutableIndexSet = NSMutableIndexSet()
    dataTestType = context.executeFetchRequest(fetchRequestForTestType, error: nil) as [TestType]

    if editingStyle == UITableViewCellEditingStyle.Delete {

        context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)
        context.save(nil)
        dataTestType.removeAtIndex(indexPath.row)

        if dataTestType.count == 0 {
            indexes.addIndex(indexPath.section)
        }

        tableView.beginUpdates()
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        tableView.deleteSections(indexes, withRowAnimation: .Fade)
        tableView.endUpdates()
    }
}

Can anyone help me?

Thank You very much!

4
  • Which line of code causes the crash, exactly?
    – matt
    Jan 21, 2015 at 20:58
  • this line inside of the commitEditingStyle function: context.deleteObject(dataTestType[indexPath.row] as NSManagedObject) Jan 21, 2015 at 20:59
  • 1
    You do know that this is not how you delete with Core Data, right? You have to delete from the managed object context, save, and stop. You are then called back in NSFetchedResultsControllerDelegate methods and that is where you update the UI.
    – matt
    Jan 21, 2015 at 21:02
  • 1
    try to make your call in this order: context.deleteObject(dataTestType[indexPath.row] as NSManagedObject) dataTestType.removeAtIndex(indexPath.row)
    – sazz
    Jan 21, 2015 at 21:04

1 Answer 1

1

You remove an object at indexPath.row from the dataTestType array

 dataTestType.removeAtIndex(indexPath.row)

And on the next line you try to get the object at the same index, but it won't exist if you deleted the only object in the array:

context.deleteObject(dataTestType[indexPath.row] as NSManagedObject)

You can simply solve the issue by reordering these two lines.

As matt pointed out, you can also use NSFetchedResultsController, as it is recommended for showing Core Data records in UITableView. Then you won't need dataTestType array at all. Now you call context.executeFetchRequest each time commitEditingStyle method is called. It is inefficient and can result in poor performance if you have many records to fetch.

7
  • Then i got another error. I will update my question! Jan 21, 2015 at 21:05
  • This error means that after deleting the object you still return 1 from tableView:numberOfRowsInSection: method in your data source. Can we see this method's implementation as well? Also if you have many sections you may have to delete the section if it contains no rows, see this answer: stackoverflow.com/a/1001239/2128900 Jan 21, 2015 at 21:11
  • Have you tried the solution proposed in my previous comment? Seems that you have delete the section when you delete the last row in that section. Jan 22, 2015 at 10:21
  • Ok I tried it but there is a new error... I will update my post again. Jan 23, 2015 at 19:24
  • You also have to change numberOfSectionsInTableView method so it returns the correct value after a section was deleted. Jan 23, 2015 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.