作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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
中删除/添加单元格,您应该使用下一个方法:
beginUpdates
通知UITableView
其大小将被更改。插入新行的信息 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
。- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
删除行。endUpdates
方法通知 UITableView
其大小已更改。关于iphone - 重新加载RowsAtIndexPaths :withRowAnimation: crashes my app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461490/
我是一名优秀的程序员,十分优秀!