2015-08-28 62 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'。 –