Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 在collectionview单元格之间切换焦点时尝试更新UI元素_Swift3_Tvos - Fatal编程技术网

Swift3 在collectionview单元格之间切换焦点时尝试更新UI元素

Swift3 在collectionview单元格之间切换焦点时尝试更新UI元素,swift3,tvos,Swift3,Tvos,我希望有人能帮我解决这个问题。以下是我的tvOS应用程序截图: 资料来源: 我试图做两件事: 当用户向下滚动遥控器时,collectionView中的第二项将自动聚焦。我需要它,这样第一个项目是重点 当用户手动在collectionView中的项目之间切换焦点时,我希望屏幕中间部分的UI元素得到更新,而不必单独按下或选择它们 以下是迄今为止我在此屏幕上的代码: import UIKit // Global variable internal let g_CR1 = "channel_cel

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

资料来源:

我试图做两件事:

当用户向下滚动遥控器时,collectionView中的第二项将自动聚焦。我需要它,这样第一个项目是重点

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

以下是迄今为止我在此屏幕上的代码:

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

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