Search code examples
swiftuitableviewuicollectionviewtvosapple-tv

Can't focus UICollectionViewCell inside UITableViewCell


so I am exploring the new tvOS. As I wanted to kind of replicate the horizontal feature section of the app store (e.g. "Best New Apps")

My plan was to use a UITableView to lay out the base structure (rows) and use an UICollectionView for all the individual horizontally scrollable items.

Unfortunately the tvOS always focuses the UITableViewCell, not the UICollectionViewCell inside. I've already read about the

override var preferredFocusedView: UIView? {
    return aView
}

So using this inside my controller is kind of hard regarding getting the correct subview.

Is there anything I could look into or could someone please point me in the right direction? Iterating through all subviews until I get the correct one seems like a bad idea.

Thanks a lot!


Solution

  • I tried out a similar situation where I had a CollectionView within a CollectionView to get the setup you described. Let's call them innerCV and outerCV. To get innerCV to be focused and not outerCV's cells, I set this delegate method to return false for outerCV.

    - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath {
        return NO;
    }
    

    In turn, innerCV's cells are now focusable as opposed to outerCV's cells.

    Credit for this goes to Apple. I used their sample project as a guide to start. Check out the Focus samples within their project.

    Sample Project: Apple tvOS UIKitCatalog