在集合视图单元格之间切换焦点时尝试更新UI元素

时间:2017-05-13 04:08:14

标签: swift3 tvos

我希望有人可以帮我解决这个问题。下面是我的tvOS应用截图:

Screenshot

我正在尝试做两件事:

  1. 当用户向下滚动遥控器时,collectionView中的第二个项目将自动聚焦。我需要这样才能使第一个项目成为焦点。

  2. 当用户在collectionView中的项目之间手动切换焦点时,我希望屏幕中间部分的UI元素得到更新,而不必单独按或选择它们。

  3. 到目前为止,这是我的代码:

    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
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我能够回答第2点。希望有人可以从中受益。不过我还是踩到了#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
    }
}