gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:19:45 25 4
gpt4 key购买 nike

我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com