I am wondering what some of the differences are between invalidateLayout()
and reloadData()
I have read the documentation here and here but am left with the following questions:
cellForItemAt indexPath
can reference the array directly with dataArray[indexPath.item]
and each data object in the array can populate the cell. reloadData()
will ensure that when a change is made to the dataArray
this is reflected in the collectionView
. With some minor testing it appears that invalidateLayout()
will also observe this change. Is this always true?invalidateLayout()
is called, will cellForItemAt indexPath
also be called for each cell as with reloadData()
?reloadData()
since Apple says to use it sparingly?Any other comments on the differences between the two functions on a fundamental level would be helpful (when to use each one, etc)
After further testing...
cellForItemAt indexPath
will be called only for cells which were not on screen but are now on screen as a result of the invalidateLayout()
.reloadData()
a lot lessIn general, it looks like reloadData()
also calls invalidateLayout()
(or at least has a similar effect) and will trigger cellForItemAt indexPath
for every cell. invalidateLayout()
, however, will only call cellForItemAt indexdPath
if the cell is now within the screen bounds and needs to be reloaded as a result of the layout change.