问题描述
我有一个奇怪的问题,我的UITableView:我使用 reloadRowsAtIndexPaths:withRowAnimation:
重新加载一些特定的行,但应用程序崩溃与一个看似无关的异常: NSInternalInconsistencyException - 尝试删除比部分中存在的更多行。
I got a strange problem with my UITableView: I use reloadRowsAtIndexPaths:withRowAnimation:
to reload some specific rows, but the app crashes with an seemingly unrelated exception: NSInternalInconsistencyException - Attempt to delete more rows than exist in section.
我的代码如下所示:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
当我替换 reloadRowsAtIndexPaths:withRowAnimation:
消息与一个简单的 reloadData
,它工作完美。
When I replace that reloadRowsAtIndexPaths:withRowAnimation:
message with a simple reloadData
, it works perfectly.
任何想法?
推荐答案
问题是, ,改变了你的表的大小。例如,您可以为表视图添加或删除一些源数据。
The problem is that you, probably, have changed size of your table. For example, you have add or remove some source data for table view.
在这种情况下,当您调用 reloadData
时,表格会完全重新加载,包括节的大小和节的数量。
In that case, when you call reloadData
your table is completely reloaded including sizes of sections and number of that sections.
但是当调用 reloadRowsAtIndexPaths:withRowAnimation:
时,表视图的参数不会重新加载。这导致下一个问题:当您尝试重新加载某些单元格表检查表视图的大小,并看到它已被更改。这会导致崩溃。此方法只能在您无法重新载入单元格视图(例如,标签已更改或您要更改其大小)时使用。
But when you call reloadRowsAtIndexPaths:withRowAnimation:
that parameters of your table view are not reloaded. That causes next problem: when you are trying to reload some cell table check the size of table view and sees that it has been changed. That results in a crash. This method can be used only when you wan't to reload view of the cell (for example, label has changed or you want to change its size).
现在,如果您希望从表视图中删除/添加单元格,您应该使用下一种方法:
Now if you want to remove/add cells from/to table view you should use next approach:
- 通知表其大小将通过调用方法
的beginUpdates
- 通知使用方法
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- 方法
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- 通知表通过调用方法
endUpdates
ofUITableView
- Inform table that its size will be changed by calling method
beginUpdates
ofUITableView
- Inform about inserting new row(s) using method
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- Inform about removing row(s) using method
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- Inform table that its size has been changed by calling method
endUpdates
ofUITableView
希望它会有所帮助
这篇关于reloadRowsAtIndexPaths:withRowAnimation:崩溃我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!