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

发布于 2024-12-01 08:17:48 字数 888 浏览 2 评论 0原文

我这里有一个很好的错误,特别是在慢速设备中。 我有一个 UITableView jusk,就像 App Store 应用程序中的那样,底部有一个“加载更多”按钮。 当按下它时,我从服务器加载新信息,设置数组,然后重新加载表视图。该信息中还包含表行中显示的每个对象的图像 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 这样的东西会很好。我尝试使用布尔锁,并在主线程中执行这两个操作,但它不起作用...... 多谢。

I have a nice bug here, specially in slow devices.
I have a UITableView jusk like the ones in the App Store app, with a Load More button at the bottom.
When it is pressed, I load the new info from the server, set the array, and reload the table view. In that info is also the url of the image for each object displayed in the table rows, so when we got it we start donwloading the image. When the image is loaded, I reload the row of the object.

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

The problem comes when the row is being reloading because the image was downloaded, and the whole table is doing so cause the user pressed the load more button and more info was gotten. Then I get this error:

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).

Which is perfectly understable, the table was enlarged while the new image row was being updated.
My question is: how can lock it so I do not reload that row until the whole table is reloaded? It would nice something like table.isReloading. I try with a boolean lock, and doing both actions in the main thread, but it did not work ...
Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦境 2024-12-08 08:17:48

好吧,文档中的一些研究让这一切有了更多的了解。

reloadRowsAtIndexPaths:withRowAnimation:如果您想提醒用户单元格的值正在更改,请调用此方法。但是,如果通知用户并不重要(也就是说,您只想更改单元格显示的值),则可以获取特定行的单元格并设置其新值。

这就是我所做的,我获取了单元格并设置了图像的新值,而不是重新加载整个图像。我想它也更有效:

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

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

它现在不会崩溃。

Well, a little research in the doc put some more light in all this.

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.

And that is what I did, I got the cell and set the new value for image, instead of reloading the whole of it. I guess it is more efficient also:

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

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

It does not crash now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文