iOS reloadRowsAtIndexPaths Crash attempt to delete row 10 from section 0 which only contains 0 rows before the update

我们通过reloadRowsAtIndexPaths进行更新某个或者某几个Cell的时候,在tableView内部先删除该Cell,再进行重新创建,如果我们在更新Cell的时候如果该Cell是不存在的就会Crash,attempt to delete row 10 from section 0 which only contains 0 rows before the update(试图删除不存在的Cell)

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

如何解决这个问题呢
1、通过使用reloadData进行完全重新加载;
2、判断该Cell是否存在,存在再进行reloadRowsAtIndexPaths,这样就可以避免Crash

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
 if (cell) {
     [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容