Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 915
danielreiser Avatar asked Oct 08 '15 15:10

danielreiser


People also ask

How to set collection view in Swift?

Add a CollectionView by pressing command shift L to open the storyboard widget window. Drag the collectionView onto the main view controller. Add constraints to the UICollectionView widget to ensure that the widget fills the screen on all devices. The foundation is now set up in the storyboard.

What is CollectionView?

A collection view manages an ordered set of content, such as the grid of photos in the Photos app, and presents it visually. Collection views are a collaboration between many different objects, including: Cells. A cell provides the visual representation for each piece of your content. Layouts.

How CollectionView works?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.


2 Answers

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

like image 155
Richard McCluskey Avatar answered Oct 07 '22 06:10

Richard McCluskey


I used a different approach..

Setting the tableview selection property to No Selection in storyboard makes the focus engine to focus on UICollectionViewCell cell rather than UITableViewCell.

Hope it helps :)

Image

like image 25
Suraj Avatar answered Oct 07 '22 04:10

Suraj