删除一行 UITableView 时 iPhone 应用程序崩溃

标签 iphone uitableview

我有一个 UITableView,它使用单例对象作为数据源。

我已经实现了以下方法来允许用户删除 UITableView 中的行。但是当我单击删除按钮时,应用程序崩溃并出现异常

方法:

-(void)viewDidLoad
{

some initialization code-----

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Delete"                                                        style:UIBarButtonItemStyleBordered                                                                 target:self                                                     action:@selector(toggleEdit:)];
       self.navigationItem.rightBarButtonItem = editButton;
       [editButton release];

}

-(IBAction)toggleEdit:(id)sender
{
    [self.myOrderTable setEditing:!self.myOrderTable.editing animated:YES];
    
    if(self.myOrderTable.editing)
        [self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    else 
    {
     [self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
    }

}

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSUInteger row = [indexPath row];
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];
    
}

但是,当我尝试单击删除按钮时,出现以下异常:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. 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                      0x010305a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01184313 objc_exception_throw + 44
    2   CoreFoundation                      0x00fe8ef8 
+[NSException raise:format:arguments:] + 136
    3   Foundation                          0x001153bb -
[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00398e8b -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8420
    5   UIKit                               0x00387cf8 -
[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 56
    6   Restaurant                          0x000117f9 -
[MyOrderViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 114
    7   UIKit                               0x00385037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
    8   UIKit                               0x0031a4fd -[UIApplication sendAction:to:from:forEvent:] + 119
    9   UIKit                               0x003aa799 -[UIControl sendAction:to:forEvent:] + 67
    10  UIKit                               0x003acc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    11  UIKit                               0x003ab7d8 -[UIControl touchesEnded:withEvent:] + 458
.............

..........
.............
)
terminate called after throwing an instance of 'NSException'

我可以从哪里开始解决这个问题?

最佳答案

“更新后现有节中包含的行数 (1) 必须等于更新前该节中包含的行数 (1),加上或减去插入的行数或从该部分删除(0 插入,1 删除)。'"

该错误清楚地说明了问题所在。我会检查您的 numberOfRowsInSection 方法。您需要考虑被删除的行。您不能总是返回相同的行数。如果您要从表中删除一行,则 numberOfRowsInSection 需要返回其旧返回值减 1。

例如:

假设我使用字符串数组来填充表格 View ;

我的 numberOfRowsInSection 方法将使用

return [array count];

我的 cellForRowAtIndexPath 方法将使用

cell.textLabel.text = (NSString*)[array objectAtIndex:indexPath.row];

当我从 tableView 中删除一行(例如行号 3)时,我也会从数组中删除该对象:

[array removeObjectAtIndex:3];

这样,当再次调用 numberOfRowsInSection 时,数组会变小一个对象,并且 numberOfRowsInSection 返回的数字会比之前少一个,这就是删除的行。

关于删除一行 UITableView 时 iPhone 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6221248/

相关文章:

ios - NSKeyedUnarchiver 导致崩溃

ios - UITableView 错误 : indexpath. row is starting from 1 instead of 0

JavaFX - 拖放 TableCell

iphone - 从基于窗口的项目开始构建基于导航的项目

iphone - 从不工作的 UIButton 创建切换?

iphone - 远程删除 iOS 中的应用程序数据

ios - UITableViewCell 不响应 setNeedsLayout

ios - UITableViewCell 根据所选行转至不同的静态 UITableViewController?

iphone - 使用 CALayer 的 UIView 不显示阴影

ios - Xcode - CorePlot 代码链接器命令错误