在UITableView中删除/插入行时出现NSInternalInconsistencyException

Rid*_*ick 2 cell uitableview ios5


我需要用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];
Run Code Online (Sandbox Code Playgroud)

但它会导致错误=(.

由于未捕获的异常'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
Run Code Online (Sandbox Code Playgroud)

有什么想法有什么不对吗?

Rid*_*ick 8

像这样的错误:

断言失败 - [_ 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文档中:

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

就我而言:insertRowsAtIndexPaths函数出现错误 .因为我的所有更改(在我的数据中)都是在执行时应用的.我的UITableViewCell的新位置是([数据计数] -1)同时我只使用[数据计数].

谢谢你的关注=).