1

如何删除节中的行UITableView

当我运行下面的代码时它会崩溃:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];
    if (editingStyle == UITableViewCellEditingStyleDelete) {


        NSIndexPath *firstRow = [NSIndexPath indexPathForRow:0 inSection:0];
        //[self.productItems removeObjectAtIndex:indexPath.row];


        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
    [tableView endUpdates];
}

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976
2013-01-19 14:46:14.847 [4752:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2.  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).'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x0174abe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0189f5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x01703628 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x0054c47b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x007cea0f -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8424
    5   UIKit                               0x007be433 -[UITableView endUpdates] + 42

    7   UIKit                               0x007bb2ea -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
    8   UIKit                               0x00751a6e -[UIApplication sendAction:to:from:forEvent:] + 119
    9   UIKit                               0x007e01b5 -[UIControl sendAction:to:forEvent:] + 67
    10  UIKit                               0x007e2647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    11  UIKit                               0x007e11f4 -[UIControl touchesEnded:withEvent:] + 458
    12  UIKit                               0x007760d1 -[UIWindow _sendTouchesForEvent:] + 567
    13  UIKit                               0x0075737a -[UIApplication sendEvent:] + 447
    14  UIKit                               0x0075c732 _UIApplicationHandleEvent + 7576
    15  GraphicsServices                    0x01c83a36 PurpleEventCallback + 1550
    16  CoreFoundation                      0x0172c064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    17  CoreFoundation                      0x0168c6f7 __CFRunLoopDoSource1 + 215
    18  CoreFoundation                      0x01689983 __CFRunLoopRun + 979
    19  CoreFoundation                      0x01689240 CFRunLoopRunSpecific + 208
    20  CoreFoundation                      0x01689161 CFRunLoopRunInMode + 97
    21  GraphicsServices                    0x01c82268 GSEventRunModal + 217
    22  GraphicsServices                    0x01c8232d GSEventRun + 115
    23  UIKit                               0x0076042e UIApplicationMain + 1160

    26  ???                                 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException' 
4

1 回答 1

4

试试这个代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView beginUpdates];    
  if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Do whatever data deletion you need to do...
  [self.productItems removeObjectAtIndex:indexPath.row];
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight ];      
  }       
  [tableView endUpdates];
}
于 2013-01-19T07:08:40.653 回答