4

I'm using Collection View Compositional Layout and need to have accurate analytics for "userViewedCell" events for orthogonally scrolling sections. I had been using collectionView(willDisplay:forItemAt) to detect when a cell is about to be shown, but this seems very buggy.

To demonstrate the problem, here is the Apple example project for Collection View Compositional Layout: https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/using_collection_view_compositional_layouts_and_diffable_data_sources

compositional layout gif

The only change I've made to the Apple code is adding this function:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
   print("will display cell at \(indexPath)")
 }

When I scroll one orthogonal section, willDisplayCell gets called for cells in the other orthogonal sections as well. For example, I scroll section 0 to cell 10, and willDisplayCell gets called for [0, 10], [1, 10], and [2, 10], even though sections 1 and 2 are only showing the first few cells.

A related problem is when I scroll section 0 all the way back to the beginning, willDisplayCell never gets called for [0, 0], [0, 1], or [0, 2].

How do I fix this in order to get accurate analytics? Or is there a different method I should be using instead?

gracie
  • 41
  • 2

0 Answers0