Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus a UICollectionViewCell in UITableViewCell in tvOS?

Description:

I have a list of UITableViewCells that have each have a UICollectionView with X amount of UICollectionViewCells (see image below for more clarity)

The idea behind this is to be able to implement sections with a header on top and a horizontal scrolling UICollectionView

Everything is working great, except when I try to focus the UICollectionViewCells (aka: CategoryCell). The UITableViewCell gets focused and highlights all the content making it impossible to focus any of the UICollectionViewCells.

What I've tried:

According to other posts the fix for this would be deactivate User Interaction and/or set the selection style to none on the UITableViewCell:

cell.selectionStyle = UITableViewCellSelectionStyle.None

and to enable User Interaction on the UICollectionViewCell instead.

Here are posts that talk about it:

UITableViewCell with UITextField losing the ability to select UITableView row?

How can I disable the UITableView selection highlighting?

I've attempted to implemen these solutions without success.

Question:

What step did I miss? Does the fix not work on tvOS? How do I focus my UICollectionViewCell that is in a UICollectionView that is in a UITableViewCell?

More:

Table View Controller

Image showing my UITableViewController and all it's contents from my StoryBoard.

like image 367
kemicofa ghost Avatar asked Jan 05 '23 09:01

kemicofa ghost


1 Answers

The problem is most likely that your table view cells are returning true to canBecomeFocused. You can disable that by either implementing the UITableViewDelegate method tableView(_, canFocusRowAt:) to return false, or by subclassing UITableViewCell and returning false from canBecomeFocused.

The focus engine will try to focus on the top-most view that returns true from canBecomeFocused, which is why it can't find your collection view cells: they're "hidden" inside the focusable table view cells. If you disable focus on the table view cells, then the focus engine will be able to find your collection view cells.

override func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return false
}
like image 51
Justin Voss Avatar answered Jan 07 '23 23:01

Justin Voss