1

Unfortunately, I have to ask this question again, because I have not found a solution yet.

At the moment I can delete it without animation, but now I want to delete it WITH animation.

My app get an error with this code:

    /*************** TABLE VIEW DELETE LEBENSMITTEL ***************/
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == .Delete) {

        let LM_ITEM = lebensmittel[indexPath.row]
        managedObjectContext!.deleteObject(lebensmittel[indexPath.row])
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        self.DatenAbrufen()

    }
}

2015-08-28 09:27:27.475 [32099:346567] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623
2015-08-28 09:27:27.483 [32099:346567] *** 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 (2) must be equal to the number of rows contained in that section before the update (2), 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).'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001091cdc65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b126bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001091cdaca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00000001098ac98f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   UIKit                               0x0000000109f37c13 -[UITableView _endCellAnimationsWithContext:] + 12678
    5   UIKit                               0x0000000119d2937b -[UITableViewAccessibility deleteRowsAtIndexPaths:withRowAnimation:] + 48
    6   App Name                        0x00000001087ca640 _TFC12App_Name26AlteLebensmittelController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 3360
    7   App Name                        0x00000001087ca887 _TToFC12App_Name26AlteLebensmittelController9tableViewfS0_FTCSo11UITableView18commitEditingStyleOSC27UITableViewCellEditingStyle17forRowAtIndexPathCSo11NSIndexPath_T_ + 87
    8   UIKit                               0x0000000109f5d1e6 -[UITableView animateDeletionOfRowWithCell:] + 132
    9   UIKit                               0x0000000109f3c3bd __52-[UITableView _swipeActionButtonsForRowAtIndexPath:]_block_invoke + 72
    10  UIKit                               0x0000000109e5bd62 -[UIApplication sendAction:to:from:forEvent:] + 75
    11  UIKit                               0x0000000109f6d50a -[UIControl _sendActionsForEvents:withEvent:] + 467
    12  UIKit                               0x0000000109f6c8d9 -[UIControl touchesEnded:withEvent:] + 522
    13  UIKit                               0x0000000109ea8958 -[UIWindow _sendTouchesForEvent:] + 735
    14  UIKit                               0x0000000109ea9282 -[UIWindow sendEvent:] + 682
    15  UIKit                               0x0000000109e6f541 -[UIApplication sendEvent:] + 246
    16  UIKit                               0x0000000109e7ccdc _UIApplicationHandleEventFromQueueEvent + 18265
    17  UIKit                               0x0000000109e5759c _UIApplicationHandleEventQueue + 2066
    18  CoreFoundation                      0x0000000109101431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    19  CoreFoundation                      0x00000001090f72fd __CFRunLoopDoSources0 + 269
    20  CoreFoundation                      0x00000001090f6934 __CFRunLoopRun + 868
    21  CoreFoundation                      0x00000001090f6366 CFRunLoopRunSpecific + 470
    22  GraphicsServices                    0x000000010de80a3e GSEventRunModal + 161
    23  UIKit                               0x0000000109e5a8c0 UIApplicationMain + 1282
    24  App Name                        0x00000001087ebb67 main + 135
    25  libdyld.dylib                       0x000000010b868145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Trombone0904
  • 4,132
  • 8
  • 51
  • 104

1 Answers1

0

The number of rows to show in your tableview is not the same you got in your datasource. Deleting a row with an animation take some time so when the deleting animation end try refreshing the tableview with yourTableview.reloadData() and remove the unused data from your datasource (before the refresh).

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourDataSource[section].count
}

Here an exemple in Objective-C:

- (void)removeRowAtIndex:(NSInteger)aIndex
{
    //Set the row to remove from the tableView
    NSMutableArray * tRemove = [NSMutableArray array];
    NSIndexPath * tIndexPath = [NSIndexPath indexPathForRow:aIndex inSection:0];
    [tRemove addObject:tIndexPath];

    //Remove the deleted data from the datasource
    NSMutableIndexSet * tRemoveIndexSet = [NSMutableIndexSet indexSet];
    [tRemoveIndexSet addIndex:aIndex];
    [YourDataSource removeObjectsAtIndexes:tRemoveIndexSet];

    //Remove the row from tableView
    [YourTableView deleteRowsAtIndexPaths:tRemove withRowAnimation:UITableViewRowAnimationLeft];
}

Swift version:

func removeRowAtIndex(aIndex:Int) {
    //Set the row to remove from the tableView
    var tRemove:Array<NSIndexPath> = Array()
    let tIndexPath:NSIndexPath = NSIndexPath(forRow: aIndex, inSection: 0)
    tRemove.append(tIndexPath)

    //Remove the deleted data from the datasource
    var tRemoveIndexSet:NSMutableIndexSet = NSMutableIndexSet()
    tRemoveIndexSet.addIndex(aIndex)
    YourDataSource.removeAtIndexes(tRemoveIndexSet)

    //OR use removeAtIndex()
    //YourDataSource.removeAtIndex(aIndex)

    //Remove the row from tableView
    YourTableView.deleteRowsAtIndexPaths(tRemove, withRowAnimation: .Left)
}

Add this extension if you want to use removeAtIndexes()

extension Array
{
    mutating func removeAtIndexes(indexes: NSIndexSet) {
        for var i = indexes.lastIndex; i != NSNotFound; i = indexes.indexLessThanIndex(i) {
            self.removeAtIndex(i)
        }
    }
}

Source : removeObjectsAtIndexes for Swift arrays

Hope this can help you :)

Community
  • 1
  • 1