由于'NSInternalInconsistencyException',TableView应用程序终止 [英] TableView app terminated due to 'NSInternalInconsistencyException'

查看:77
本文介绍了由于'NSInternalInconsistencyException',TableView应用程序终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解UITableViews以及随之而来的一切。目前我有以下代码:

I'm trying to get the hang of UITableViews and everything that goes with it. At the moment I have the following code:

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }

     [cell.textLabel setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];

     return cell; 
}

- (IBAction)killItem {
     NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:2 inSection:0];
     [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationRight];
}

启动killItem函数时出现以下错误:

And I get the following error when initiating the "killItem" function:


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

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 (10) must be equal to the number of rows contained in that section before the update (10), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

我理解的方式tableViews基本上有一个委托和一个数据源,其中数据源确定了多少行应该在tableView中。通过stackoverflow的一些搜索,我发现当数据源与现实不匹配时,当它搜索不存在的行时,我已经删除了这个错误。

The way I understand it tableViews basically have a delegate and a data source where the data source, among other things, determines how many rows should be in the tableView. Through some searches here at stackoverflow I've found that this error is caused when the "data source doesn't match reality", when it's searching for rows that don't exist, that I have deleted.

我可能已经弄错了,但这就是我的想法。所以我的问题是,如何让这些匹配以便我可以避免这个错误?

I might have gotten this wrong but that's what I think is doing it. So my question is, how do I get these to match so that I can avoid this error?

作为参考,我已经查看了以下帖子而没有理解什么我需要这样做:

For reference, I've looked in to the following posts without understanding what I need to do:

错误:iPhone SDK中UITableView中的部分行数

滑动UITableViewCell

由于未捕获的异常'NSRangeException'终止应用程序,原因:'*** - [NSMutableArray objectAtIndex:]:索引1超出边界[0。 .0]'

添加 [tbl reloadData] [tbl killItem函数中的[tbl endUpdate] ,似乎没有帮助我的问题。

Adding [tbl reloadData], [tbl beginUpdate] ... [tbl endUpdate] in the killItem function, doesen't seem to help my problem eighter.

Tha你提前,
Tobias Tovedal

Thank you in advance, Tobias Tovedal

推荐答案

这很简单,问题出在这里:

It's pretty easy, the problem lies here:

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

apple使用的委托模式意味着你是负责人通过其委托来管理UITableView的内容,这意味着,如果删除一行,您还要负责从数据模型中删除数据。

The delegate pattern used by apple, means that you're the one responsible on managing the content of the UITableView through its delegates, meaning that, if you delete a row, you're also responsible of deleting the data from the data model.

所以,在删除一行之后,将部分中的行数减少到9是有意义的,但是,您的函数总是返回10,从而抛出异常。

So, after deleting a row, it would make sense that the number of rows in section would decrease to "9", yet, your function is always returning 10, and thus throwing the exception.

通常,当使用表格并且内容会发生变化时,NSMutableArray很常见,您可以这样做:

Typically, when using an table, and the contents will change, an NSMutableArray is pretty common, you do something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayWithStuff count];
}

然后,删除一个对象( removeObjectAtIndex:<来自数组的/ code>)会自动更新行数。

And then, deleting an object (removeObjectAtIndex:) from the array would automatically update the number of rows.

(编辑:与 Mike Hay大约同时回复,请尝试遵循他的建议!我跳过了开始/结束更新,因为看起来你已经读过它了)

( Replied at about the same time as Mike Hay, try following his advice too! I skipped the begin/end Update, because it seems you already read about it)

这篇关于由于'NSInternalInconsistencyException',TableView应用程序终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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