PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 29, 2022

[FIXED] How to focus a UICollectionViewCell in UITableViewCell in tvOS?

 September 29, 2022     swift, tvos, uicollectionview, uicollectionviewcell, uitableview     No comments   

Issue

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.


Solution

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
}


Answered By - Justin Voss
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing