【问题标题】:NSInternalInconsistencyException while deleting/inserting rows in UITableView在 UITableView 中删除/插入行时出现 NSInternalInconsistencyException
【发布时间】:2011-12-09 07:16:36
【问题描述】:


我需要用 tableView 创建一个应用程序。现在我可以加载/重新加载表中的所有数据。添加/删除操作 tableView 有编辑模式。但问题是在这种模式下 tableView 显示标准的添加/删除按钮。但我不需要这个按钮。在 Apple 教程中,我发现了其他添加/删除行的方法,如下所示:

[data removeObjectAtIndex:0];
[data addObject:[[chooseTypeCell alloc]init]];
NSIndexPath *insertIndexPaths=[NSIndexPath indexPathForRow:[data count] inSection:0]; 
NSIndexPath *deleteIndexPaths=[NSIndexPath indexPathForRow:0 inSection:0];
[[self view] beginUpdates];
[[self view] insertRowsAtIndexPaths:[NSArray arrayWithObject:insertIndexPaths] withRowAnimation:UITableViewRowAnimationRight];
[[self view] deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPaths] withRowAnimation:UITableViewRowAnimationFade];
[[self view] endUpdates];

但它会导致错误 =(.

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效的表视图更新。应用程序请求更新与数据源提供的状态不一致的表视图。'

在我的例子中,我使用对象,它是 UITableViewController 的子类,带有 UITableViewdataSource 和 UITableViewDelegate 协议。在这个对象中,视图变量与我的 UITableView 相连。结果,UITableView 连接到它唯一的控制器。在这个控制器中,我定义了这些方法:

#pragma mark - Table view data source

#######>>>>> right code =)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


#######>>>>> in comment section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

#######>>>>> without any code or cap

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

#pragma mark - Table view delegate

#######>>>>> right code =)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
#######>>>>> in comment section

 - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 

 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

#######>>>>> without any code or cap

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath
*)indexPath

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath
*)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

有什么想法吗?

【问题讨论】:

    标签: uitableview ios5 cell


    【解决方案1】:

    这样的错误:

    -[_UITableViewUpdateSupport _computeRowUpdates] 中的断言失败,/SourceCache/UIKit/UIKit-1912.3/UITableViewSupport.m:386 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效的表视图 更新。应用程序已请求更新表视图 与数据源提供的状态不一致。'

    可能会出现,因为您的 UITable-Data 类定义错误。 您需要注意这些方法:

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    您还需要检查您的动画代码。所有索引必须取自结果数据结构

    在 Apple 文档中:

    动画块中的删除和重新加载操作指定 应删除原始表中的哪些行和部分,或 重新加载;插入指定应添加哪些行和部分 到结果表。用于标识部分的索引路径和 行遵循此模型。在可变对象中插入或删除项目 另一方面,数组可能会影响用于 连续插入或移除操作;例如,如果您插入 某个索引处的项目,该索引中所有后续项目的索引 数组递增。

    在我的例子中:insertRowsAtIndexPaths 函数中出现错误。因为我的所有更改(在我的数据中)都是在执行时应用的。我的 UITableViewCell 的新位置是 ([data count]-1) 同时我只使用了 [data count]。

    感谢您的关注 =)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      相关资源
      最近更新 更多