deleteRowsAtIndexPaths 崩溃

发布于 2024-10-02 12:39:07 字数 4975 浏览 3 评论 0原文

当我从表中删除行时,我的应用程序崩溃了。这是我检测到错误和堆栈跟踪的来源。谢谢!

//delete row from database
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"\ncommitEditingStyle");

    //delete user from Users table with specified ID
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    {

        //Get the object to delete from the array.
        [dbManager open: @DB_FILE_NAME];    

        //get userID from array usersIDList[row] = userID!
        NSNumber *usersID = [usersIDList objectAtIndex: [indexPath row]];
        [dbManager deleteUserWithID: [usersID intValue] table: @TABLE_USERS fieldName: @"UserID" ];

        [dbManager close];

        //Delete the object from the table.
        [self.tableView deleteRowsAtIndexPaths:
         [NSArray arrayWithObject: indexPath] 
        withRowAnimation: UITableViewRowAnimationFade];
    }
}

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    NSLog(@"\ndeleteRowsAtIndexPaths");
}

[Session started at 2010-11-15 18:01:23 +0200.]
2010-11-15 18:01:25.052 PhoneBook[12397:207] ******* Accessibility Status Changed: On
2010-11-15 18:01:25.101 PhoneBook[12397:207] ********** Loading AX for: com.yourcompany.PhoneBook ************
2010-11-15 18:01:30.060 PhoneBook[12397:207] 
deleteBtnUserClick
2010-11-15 18:01:31.878 PhoneBook[12397:207] 
commitEditingStyle
2010-11-15 18:01:31.881 PhoneBook[12397:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920
2010-11-15 18:01:31.883 PhoneBook[12397:207] *** 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 (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02510b99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0266040e objc_exception_throw + 47
    2   CoreFoundation                      0x024c9238 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000b8e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00338d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719
    5   UIKit                               0x00328511 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
    6   UIKit                               0x0aaca8fc -[UITableViewAccessibility(Accessibility) deleteRowsAtIndexPaths:withRowAnimation:] + 74
    7   PhoneBook                           0x00003491 -[RootViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 392
    8   UIKit                               0x0032596d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
    9   UIKit                               0x0aacde92 -[UITableViewCellAccessibility(SafeCategory) deleteConfirmationControlWasClicked:] + 62
    10  UIKit                               0x002be7f8 -[UIApplication sendAction:to:from:forEvent:] + 119
    11  UIKit                               0x00349de0 -[UIControl sendAction:to:forEvent:] + 67
    12  UIKit                               0x0034c262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    13  UIKit                               0x0034ae0f -[UIControl touchesEnded:withEvent:] + 458
    14  UIKit                               0x002e23d0 -[UIWindow _sendTouchesForEvent:] + 567
    15  UIKit                               0x002c3cb4 -[UIApplication sendEvent:] + 447
    16  UIKit                               0x002c89bf _UIApplicationHandleEvent + 7672
    17  GraphicsServices                    0x02d6c822 PurpleEventCallback + 1550
    18  CoreFoundation                      0x024f1ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    19  CoreFoundation                      0x02452807 __CFRunLoopDoSource1 + 215
    20  CoreFoundation                      0x0244fa93 __CFRunLoopRun + 979
    21  CoreFoundation                      0x0244f350 CFRunLoopRunSpecific + 208
    22  CoreFoundation                      0x0244f271 CFRunLoopRunInMode + 97
    23  GraphicsServices                    0x02d6b00c GSEventRunModal + 217
    24  GraphicsServices                    0x02d6b0d1 GSEventRun + 115
    25  UIKit                               0x002ccaf2 UIApplicationMain + 1160
    26  PhoneBook                           0x000026ec main + 102
    27  PhoneBook                           0x0000267d start + 53
)
terminate called after throwing an instance of 'NSException'

My application crashes when I'm deleting row from table. Here is my sources where the bug is detected and stack trace. Thanx!

//delete row from database
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"\ncommitEditingStyle");

    //delete user from Users table with specified ID
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    {

        //Get the object to delete from the array.
        [dbManager open: @DB_FILE_NAME];    

        //get userID from array usersIDList[row] = userID!
        NSNumber *usersID = [usersIDList objectAtIndex: [indexPath row]];
        [dbManager deleteUserWithID: [usersID intValue] table: @TABLE_USERS fieldName: @"UserID" ];

        [dbManager close];

        //Delete the object from the table.
        [self.tableView deleteRowsAtIndexPaths:
         [NSArray arrayWithObject: indexPath] 
        withRowAnimation: UITableViewRowAnimationFade];
    }
}

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    NSLog(@"\ndeleteRowsAtIndexPaths");
}

[Session started at 2010-11-15 18:01:23 +0200.]
2010-11-15 18:01:25.052 PhoneBook[12397:207] ******* Accessibility Status Changed: On
2010-11-15 18:01:25.101 PhoneBook[12397:207] ********** Loading AX for: com.yourcompany.PhoneBook ************
2010-11-15 18:01:30.060 PhoneBook[12397:207] 
deleteBtnUserClick
2010-11-15 18:01:31.878 PhoneBook[12397:207] 
commitEditingStyle
2010-11-15 18:01:31.881 PhoneBook[12397:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920
2010-11-15 18:01:31.883 PhoneBook[12397:207] *** 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 (6) must be equal to the number of rows contained in that section before the update (6), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02510b99 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0266040e objc_exception_throw + 47
    2   CoreFoundation                      0x024c9238 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x000b8e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00338d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719
    5   UIKit                               0x00328511 -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
    6   UIKit                               0x0aaca8fc -[UITableViewAccessibility(Accessibility) deleteRowsAtIndexPaths:withRowAnimation:] + 74
    7   PhoneBook                           0x00003491 -[RootViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 392
    8   UIKit                               0x0032596d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
    9   UIKit                               0x0aacde92 -[UITableViewCellAccessibility(SafeCategory) deleteConfirmationControlWasClicked:] + 62
    10  UIKit                               0x002be7f8 -[UIApplication sendAction:to:from:forEvent:] + 119
    11  UIKit                               0x00349de0 -[UIControl sendAction:to:forEvent:] + 67
    12  UIKit                               0x0034c262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    13  UIKit                               0x0034ae0f -[UIControl touchesEnded:withEvent:] + 458
    14  UIKit                               0x002e23d0 -[UIWindow _sendTouchesForEvent:] + 567
    15  UIKit                               0x002c3cb4 -[UIApplication sendEvent:] + 447
    16  UIKit                               0x002c89bf _UIApplicationHandleEvent + 7672
    17  GraphicsServices                    0x02d6c822 PurpleEventCallback + 1550
    18  CoreFoundation                      0x024f1ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    19  CoreFoundation                      0x02452807 __CFRunLoopDoSource1 + 215
    20  CoreFoundation                      0x0244fa93 __CFRunLoopRun + 979
    21  CoreFoundation                      0x0244f350 CFRunLoopRunSpecific + 208
    22  CoreFoundation                      0x0244f271 CFRunLoopRunInMode + 97
    23  GraphicsServices                    0x02d6b00c GSEventRunModal + 217
    24  GraphicsServices                    0x02d6b0d1 GSEventRun + 115
    25  UIKit                               0x002ccaf2 UIApplicationMain + 1160
    26  PhoneBook                           0x000026ec main + 102
    27  PhoneBook                           0x0000267d start + 53
)
terminate called after throwing an instance of 'NSException'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

节枝 2024-10-09 12:39:07

我以前见过这个,这肯定是您忘记更新填充表的数据源。但这部分代码丢失了。
导致这个问题的方法是

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ;
}

I've seen this before and it is definitely that you have forgotten to update source of data which fill up table. But that part of code is missing.
The method which caused this is

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ;
}

吾性傲以野 2024-10-09 12:39:07

是的,问题出在方法 numberOfRowsInSection
因此,为了避免麻烦,您应该:

  1. 在函数 commitEditingStyle 中从数组、数据库等中删除数据
  2. 。减少当前的行数。
  3. [tableView beginUpdates];
  4. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];

  5. [tableView endUpdates] ;

以及所有没事!

Yes, the problem was in method numberOfRowsInSection
So, to leave troubles you should:

  1. In function commitEditingStyle delete data from your array, database etc.
  2. Decrement your current row count.
  3. [tableView beginUpdates];
  4. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];

  5. [tableView endUpdates];

And all be OK!

妄想挽回 2024-10-09 12:39:07

更新了 Swift 4.2

如果您要删除部分中的最后一行,则应该删除整个部分而不是行。为此:

    tableView.beginUpdates()
    dataArray.remove(at: row)
    if tableView.numberOfRows(inSection: section) == 1 {
        tableView.deleteSections(IndexSet(integer: section), with: .automatic)
    }
    else {
        tableView.deleteRows(at: indexPath, with: .fade)
    }
    tableView.endUpdates()

Updated Swift 4.2

If you are about to remove last row in section, you should remove entire section instead of row. To do that:

    tableView.beginUpdates()
    dataArray.remove(at: row)
    if tableView.numberOfRows(inSection: section) == 1 {
        tableView.deleteSections(IndexSet(integer: section), with: .automatic)
    }
    else {
        tableView.deleteRows(at: indexPath, with: .fade)
    }
    tableView.endUpdates()
放我走吧 2024-10-09 12:39:07

从错误消息中可以很清楚地看出:

'无效更新:无效数量
第 0 节中的行数。行数
包含在现有部分之后
更新(6)必须等于
其中包含的行数
更新之前的部分 (6),加上或
减去插入的行数或
从该部分删除(插入 0,
1 已删除)。'

您尝试删除的对象不会在您的数据源中删除。
如果我假设您的 UITableViewDataSource 也使用 dbManager,则必须修复 dbManager 才能实际删除该对象。

Pretty clear from the error message:

'Invalid update: invalid number of
rows in section 0. The number of rows
contained in an existing section after
the update (6) must be equal to the
number of rows contained in that
section before the update (6), plus or
minus the number of rows inserted or
deleted from that section (0 inserted,
1 deleted).'

The object you are trying to delete doesn't get deleted in your datasource.
If I assume that your UITableViewDataSource uses dbManager too, you have to fix dbManager to actually delete the object.

吃兔兔 2024-10-09 12:39:07

我遇到了同样的错误,原因是我删除了表中的最后一行,
我的 numberOfSectionsInTableView 实现返回 rowsArray 计数为 0..

更改为最小值 1 解决了这个问题

I got the same error and reason was I delete the last row in the table and
my numberOfSectionsInTableView implementation return rowsArray count which was 0..

changing to minimum of 1 solve the case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文