1
votes

I have a controller implement with UICollectionView, and it has two sections. The section datas is fetch from two different API, and the return order is unknown.

When the first section data is returned, I call reloadSections to update first sections' cell. When the second section data is returned, I call reloadData to update whole cells(including the first section and the second section)

But if i call reloadData before reloadSections, the cell update correctly. When calling reloadSections before reloadData, the items count in second section is correct, but the cell shows the duplicate content, not the new content.

I have write a demo project and insert new data to second section, call reloadData, insert new data to first section. the collection view can also work correctly.

It just not works when i call reloadSections:FirstSection, then calling reloadData.

The Demo project can be find here: https://github.com/6david9/ReloadData/blob/master/ReloadData/ReloadData/ViewController.m

p.s. When i call reloadSections for each section update, the result is correct!

p.s. When i call reloadSections for the first section, it reload all the cells in first section. 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. so it show duplication content of the last item previous was the last one.

2

2 Answers

2
votes

The problem is when the reload data is called the collection view only call

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 e.g. 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

0
votes

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