iphone - 重新加载RowsAtIndexPaths :withRowAnimation: crashes my app

标签 iphone uitableview crash

我的 UITableView 遇到了一个奇怪的问题:我使用 reloadRowsAtIndexPaths:withRowAnimation: 重新加载某些特定行,但应用程序因看似不相关的异常而崩溃:NSInternalInconsistencyException - 尝试删除更多行部分中存在的行数。

我的代码如下所示:

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];

当我用简单的 reloadData 替换 reloadRowsAtIndexPaths:withRowAnimation: 消息时,它工作得很好。

有什么想法吗?

最佳答案

问题是您可能更改了 UITableView 数据源的项目数。例如,您在 UITableViewDataSource 协议(protocol)实现中使用的数组或字典中添加或删除了一些元素。

在这种情况下,当您调用 reloadData 时,您的 UITableView 将完全重新加载,包括节数和行数。

但是当您调用reloadRowsAtIndexPaths:withRowAnimation:时,这些参数不会重新加载。这会导致下一个问题:当您尝试重新加载某些单元格时,UITableView 检查数据源的大小并发现它已更改。这会导致崩溃。仅当您想要重新加载单元格的内容 View 时(例如,标签已更改或您想要更改其大小),才可以使用此方法。

现在,如果您想从 UITableView 中删除/添加单元格,您应该使用下一个方法:

  1. 通过调用方法beginUpdates通知UITableView其大小将被更改。
  2. 告知有关使用方法插入新行的信息 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
  3. 告知如何使用方法 - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 删除行。
  4. 通过调用 endUpdates 方法通知 UITableView 其大小已更改。

关于iphone - 重新加载RowsAtIndexPaths :withRowAnimation: crashes my app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461490/

相关文章:

iphone - 适用于 Mac 的 MPMoviePlayerController?

iOS 在 main.m 中崩溃,如何查找错误?

android - 禁用设置按钮后,Android我的应用程序崩溃

swift - 分段 Controller

r - 如何让R崩溃?

iphone - 创建本地化的 iPhone 应用程序,但允许用户更改应用程序的语言

iphone - 将 UITableView 分隔符隐藏在 contentView 后面

iphone - 如何在 MKMapView (SDK3.2) 中使用 UIPopoverController 在 iPad 上关闭 PopoverAnimated

ios - 按 UiTableView 索引上的放大镜图标

ios - 传递从自定义 tableView Cell 定义的对象