删除一行UITableView时,iPhone-App崩溃 [英] iPhone-App crashes when a row of UITableView is deleted

查看:118
本文介绍了删除一行UITableView时,iPhone-App崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableView which uses a singleton object as a datasource.

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

I have implemented the following methods to allow a user to delete the rows in the UITableView. But when I click the delete button the app crashes with a exception

方法:

-(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];

}

但是当我尝试点击时,我收到以下异常删除按钮:

However I get the following exception when I try to click on the delete button:


断言失败 - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-1448.89 / UITableView.m:995

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

因未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第1节中的行数无效。现有部分中包含的行数update(1)必须等于update(1)之前该部分中包含的行数,加上或减去从该部分插入或删除的行数(0插入,1删除)。'

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'

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

Where I can begin to solve this problem?

推荐答案

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

错误清楚地说明怎么了。我会检查你的 numberOfRowsInSection 方法。您需要考虑要删除的行。您不能总是返回相同数量的行。如果你从表中删除一行,你的numberOfRowsInSection需要返回它的旧返回值减去1。

The error clearly says what's wrong. I would examine your numberOfRowsInSection method. You need to account for the row being deleted. You can't always return the same number of rows. If you're deleting one row from the table, your numberOfRowsInSection needs to be returning it's old return value minus 1.

例如:

假设我使用一个字符串数组来填充tableview;

Let's say I use an array of strings to populate the tableview;

我的numberOfRowsInSection方法将使用

My numberOfRowsInSection method would use

return [array count];

我的cellForRowAtIndexPath方法将使用

My cellForRowAtIndexPath method would use

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

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

When I delete a row (say row number 3) from the tableView, I would remove the object from the array as well:

[array removeObjectAtIndex:3];

这样当再次调用numberOfRowsInSection时,数组是一个较小的对象,numberOfRowsInSection返回一个数字少于之前,它占已删除的行。

这篇关于删除一行UITableView时,iPhone-App崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆