美文网首页
iOS reloadRowsAtIndexPaths Crash

iOS reloadRowsAtIndexPaths Crash

作者: 码哥进化 | 来源:发表于2017-09-28 14:28 被阅读576次

我们通过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];
 }

相关文章

网友评论

      本文标题:iOS reloadRowsAtIndexPaths Crash

      本文链接:https://www.haomeiwen.com/subject/amnmextx.html