Home / IOS Development / ios – tvOS: Unable to browse UICollectionView elements when implemented as follows: UICollectionView inside UITableView

ios – tvOS: Unable to browse UICollectionView elements when implemented as follows: UICollectionView inside UITableView

I have implemented this:
enter image description here
By following this guide.
Here is the problem:

  • I can not scroll through the collectibles.

I think this has something to do with the fact that the project I followed is for iOS and my project is for tvOS.
I have found a somewhat similar question. An answer related to this GitHub Repo whose implementation does not seem so different from mine.

Here is the relevant code:


class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
} func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TableViewCell else { fatalError("Unable to create explore table view cell")} return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 140 } }


class TableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        // Initialization code
        collectionView.delegate = self
        collectionView.dataSource = self

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)


extension TableViewCell:  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 50
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
        return cell
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // For some reason he chose the measures of collectionViewCell and substracted 2
        return CGSize(width: 139, height: 64)
    // Highlight the current cell
    // This doesn't work
       func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
           if let pindex  = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: pindex) {
               cell.contentView.layer.borderWidth = 0.0
               cell.contentView.layer.shadowRadius = 0.0
               cell.contentView.layer.shadowOpacity = 0.0
           if let index  = context.nextFocusedIndexPath, let cell = collectionView.cellForItem(at: index) {
               cell.contentView.layer.borderWidth = 8.0
               cell.contentView.layer.borderColor = UIColor.orange.cgColor
               cell.contentView.layer.shadowColor = UIColor.orange.cgColor
               cell.contentView.layer.shadowRadius = 10.0
               cell.contentView.layer.shadowOpacity = 0.9
               cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
               collectionView.scrollToItem(at: index, at: [.centeredHorizontally, .centeredVertically], animated: true)

In a separate project, I have implemented a collectionView independently. That is: It was not built into a table view. And that works just fine.

I copied the code from the project that highlights the selected cell and added it tableViewCell.swift but it had no effect at all.

So my question is:

  • Why can’t I select a cell and / or browse collectionView?

Source link