简体   繁体   English

UICollectionView reloadSections之后:和reloadData单元未正确更新

[英]UICollectionView after reloadSections: and reloadData the cell is not update correctly

I have a controller implement with UICollectionView, and it has two sections. 我有一个带有UICollectionView的控制器实现,它有两个部分。 The section datas is fetch from two different API, and the return order is unknown. 节数据是从两个不同的API获取的,返回顺序未知。

When the first section data is returned, I call reloadSections to update first sections' cell. 返回第一节的数据时,我调用reloadSections来更新第一节的单元格。 When the second section data is returned, I call reloadData to update whole cells(including the first section and the second section) 当返回第二部分数据时,我调用reloadData以更新整个单元格(包括第一部分和第二部分)

But if i call reloadData before reloadSections, the cell update correctly. 但是,如果我在reloadSections之前调用reloadData,则单元格将正确更新。 When calling reloadSections before reloadData, the items count in second section is correct, but the cell shows the duplicate content, not the new content. 在reloadData之前调用reloadSections时,第二部分中的项目计数是正确的,但是单元格显示的是重复内容,而不是新内容。

I have write a demo project and insert new data to second section, call reloadData, insert new data to first section. 我已经编写了一个演示项目,并将新数据插入第二部分,调用reloadData,将新数据插入第一部分。 the collection view can also work correctly. 集合视图也可以正常工作。

It just not works when i call reloadSections: FirstSection , then calling reloadData. 当我调用reloadSections: FirstSection ,然后调用reloadData时,它只是不起作用。

The Demo project can be find here: https://github.com/6david9/ReloadData/blob/master/ReloadData/ReloadData/ViewController.m 该演示项目可以在这里找到: https : //github.com/6david9/ReloadData/blob/master/ReloadData/ReloadData/ViewController.m

ps When i call reloadSections for each section update, the result is correct! ps当我为每个部分更新调用reloadSections时,结果是正确的!

ps When i call reloadSections for the first section, it reload all the cells in first section. ps当我为第一部分调用reloadSections时,它将重新加载第一部分中的所有单元格。 But after then, i call reloadData it only calls cellForItem for the last item in second section, which i add new item to the front of the second section. 但是在那之后,我调用reloadData,它只调用第二部分最后一个项目的cellForItem,这会将新项目添加到第二部分的前面。 so it show duplication content of the last item previous was the last one. 因此它显示了上一个项目的最后一个的重复内容。

The problem is when the reload data is called the collection view only call 问题是当重载数据被称为仅收集视图调用时

cellForItemAtIndexPath cellForItemAtIndexPath

for the second list and for the last index in the second list. 用于第二个列表和第二个列表中的最后一个索引。 So your data is duplicate. 因此,您的数据是重复的。 It should reload the whole collection view but it does not do it. 它应该重新加载整个集合视图,但不会这样做。 I think for some performance reason or it is something else. 我认为出于某种性能原因或其他原因。 Also in the reload documentation they are saying 同样在重新加载文档中,他们说

"don't call it in the middle of an animation eg insertion or deletion" “不要在动画中间调用它,例如插入或删除”

You are calling it in that process, So it is some performance efficiency from collection view. 您在该过程中称呼它,因此从集合的角度来看这具有一定的性能效率。 Try to call these insertion on some other occasion once the collection view reloads it completely. 收集视图完全重载后,请尝试在其他情况下调用这些插入。 I ran your project and from break point checking I found this conclusion. 我运行了您的项目,从断点检查发现了这个结论。 Good luck 祝好运

but the cell shows the duplicate content, not the new content. 但单元格显示重复内容,而不是新内容。

most common reason, that you must clear cell content if you reuse cell. 最常见的原因是,如果您重复使用单元格,则必须清除单元格内容。 For example: you should remove programmatically created views or clear field or labels values... 例如:您应该删除以编程方式创建的视图或清除字段或标签值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM