2015-08-28 79 views
1

不幸的是,我不得不再次提出這個問題,因爲我還沒有找到解決方案。用動畫快速刪除行

目前我可以不使用動畫將其刪除,但現在我想用動畫將其刪除。

我的應用得到一個錯誤與此代碼:

/*************** 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) 

回答

0

顯示的行數在你的tableview是不是你在你的數據源得到了相同的。用動畫刪除一行需要一些時間,所以當刪除動畫結束時,嘗試使用yourTableview.reloadData()刷新表格視圖並從數據源中刪除未使用的數據(在刷新之前)。

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

這裏的爲例在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]; 
} 

斯威夫特版本:

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) 
} 

,如果你想使用removeAtIndexes()

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

添加該擴展來源:removeObjectsAtIndexes for Swift arrays

希望這可以幫助你:)

+0

我怎麼能檢查當動畫結束? – Ghost108

+0

或你是什麼意思? – Ghost108

+0

您無法真正檢查刪除動畫的結尾,因此我使用了一種方法在tableView中刪除或添加單元格,而不是'commitEditingStyle'。 –