Search code examples
iosswiftuicollectionviewtvos

UICollectionView How to find the row that a cell ends up in


So, I have a tvOS app where I have a UICollectionView. I'm able to use the following delegate callback to the the IndexPath of the next focused cell.

override func collectionView(didUpdateFocusIn context: ...)

The CollectionView has one Section and n number of cells that fall into it. So, when I get an IndexPath of [0,8] all that tells me is that it's the 9th item from the left. But, the first row only has 8 cells, so in reality its the first item on the left side of the second row.

How do I determine what row it's actually rendered on?


Solution

  • Can you show your code, the behavior, and the desired behavior? Also a screen shot will help.

    If I understand your question correctly...

    What determines the row the cell appears up on are these factors: 1. The size of the view 2. The size of the cell 3. collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt 4. The space between each cell

    An example is, if you have a portrait orientation, with a view width of 800, and 8 cells width 100 each, and space between each cell is 10, your 8th cell will be on the second row.

    One solution is to account for the width of the cell based on the (view.frame.width / numOfCells on each row) and account for the space between each cell.