Search code examples
swift3tvos

Trying to update UI elements when switching focus between collectionview cells


I hope someone can help me with this problem. Below is my tvOS app screenshot:

Screenshot
(source: mtiaz.com)
.

I am trying to do two things:

  1. When the user scrolls down on their remote, the second item in the collectionView becomes automatically focused. I need to have it so that the first item is focused instead.

  2. When the user manually shifts focus between items in the collectionView, I want the UI elements in the mid-section of the screen to get updated instead of having to press or select them individually.

Here is my code so far for this screen:

import UIKit

// Global variable
internal let g_CR1 = "channel_cell_01"

class DashboardVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    // outlets
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var lblChannelName: UILabel!
    @IBOutlet weak var imgChannelLogo: UIImageView!
    @IBOutlet weak var txtChannelDescription: UITextView!

    // variables
    var staticData: [Channel] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // sample channels
        initializeSampleData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // MARK: - CollectionView Callbacks
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return staticData.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        // Get or make a cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: g_CR1, for: indexPath) as! CollectionViewCell

        // Configure
        cell.imgView.backgroundColor = UIColor.black
        cell.imgView.image = staticData[indexPath.row].chLogo

        // Return.
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return true
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let currentChannel = staticData[indexPath.row]

        DispatchQueue.main.async {
            self.lblChannelName.text = currentChannel.chName
            self.imgChannelLogo.image = currentChannel.chLogo
            self.txtChannelDescription.text = currentChannel.chDescription
        }
    }

}

Solution

  • I was able to answer point #2. Hopefully someone may benefit from this. However I am still stomped at #1.

    func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
    
        // test
        print("Previous Focused Path: \(context.previouslyFocusedIndexPath)")
        print("Next Focused Path: \(context.nextFocusedIndexPath)")
    
        let currentChannel = staticData[(context.nextFocusedIndexPath?[1])!]
    
        DispatchQueue.main.async {
            self.lblChannelName.text = currentChannel.chName
            self.imgChannelLogo.image = currentChannel.chLogo
            self.txtChannelDescription.text = currentChannel.chDescription
        }
    }