同时重新加载行和整个表时崩溃

问题描述:

我在这里有一个很好的bug,特别是在慢速设备中。 我有一个UITableView,就像App Store应用程序中的一样,底部有一个Load More按钮。 当它被按下时,我从服务器加载新的信息,设置数组,并重新加载表格视图。在那个信息中,也是表格行中显示的每个对象的图像的URL,所以当我们得到它时,我们开始重新载入图像。当图像加载时,我重新加载对象的行。同时重新加载行和整个表时崩溃

[self.searchResultsTableView reloadRowsAtIndexPaths: [NSArray arrayWithObject: path] withRowAnimation: UITableViewRowAnimationNone]; 

问题是当该行被重新加载,因为图像被下载,整个表这样做会导致用户按下负载更多按钮,更多信息被得到。然后我得到这个错误:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (140) must be equal to the number of rows contained in that section before the update (125), plus or minus the number of rows inserted or deleted from that section (1 inserted, 1 deleted). 

这是完全可以理解的,表被放大,而新的图像行正在更新。 我的问题是:如何锁定它,所以我不重新加载该行,直到整个表重新加载?它会很好,像table.isReloading。我尝试使用布尔锁,并在主线程中执行这两个操作,但它不起作用... 非常感谢。

好吧,在文档中的一点研究,在这一切中提供了更多的光。

reloadRowsAtIndexPaths:withRowAnimation: Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user is not important—that is, you just want to change the value that a cell is displaying—you can get the cell for a particular row and set its new value.

这就是我做什么,我得到了细胞,并为图像的新的价值,而不是重新加载整个的它。我想这是更有效也:

AHMyTableViewCell *cell = (AHMyTableViewCell *) [self.myTableView cellForRowAtIndexPath:path]; 

       if ((cell != nil) && ([cell isKindOfClass:[AHMyTableViewCell class]])) 
        cell.myImageView.image = image; 

它现在不崩溃。