如何获得';家长';使用swift-tvOS在UIcollectionview单元中创建聚焦单元

如何获得';家长';使用swift-tvOS在UIcollectionview单元中创建聚焦单元,swift,uicollectionview,uicollectionviewcell,tvos,Swift,Uicollectionview,Uicollectionviewcell,Tvos,我为我的tvOS应用程序制作了某种Netflix布局,其中包含几个名为:featuredCollectionView和standardCollectionView的集合视图 我有一个包含当前聚焦单元格的变量。我唯一想要的是获取所选单元格的当前集合视图。有人能帮我吗 代码 func pressedThePlayPauseButton() { if let focusedCell = UIScreen.main.focusedView as? UICollectionViewCell{

我为我的tvOS应用程序制作了某种Netflix布局,其中包含几个名为:
featuredCollectionView
standardCollectionView
的集合视图

我有一个包含当前聚焦单元格的变量。我唯一想要的是获取所选单元格的当前集合视图。有人能帮我吗

代码

func pressedThePlayPauseButton() {

   if let focusedCell = UIScreen.main.focusedView as? UICollectionViewCell{

       let collectionViewOfFocusedCell = ...

始终可以在视图层次结构上漫游:

    var parentCollectionView = self.superview
    while parentCollectionView is UICollectionView != true {
        parentCollectionView = parentCollectionView?.superview
    }
如果需要查看多个集合视图,可以将上面的UICollectionView更改为要查找的子类。更简单的方法是在cellForItemAtIndexPath中为单元格提供对collectionView的弱引用;这也适用于视图控制器,而不是您真正想要引用的collectionView。它必须很弱,因为collectionView保留了单元格;否则将创建一个循环

    class CustomCollectionViewCell: UICollectionViewCell {
        weak var customCollectionViewController: CustomCollectionViewController?

    }

    class CustomCollectionViewController: UICollectionViewController {
        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: CustomCollectionViewCell.self), for: indexPath) as! CustomCollectionViewCell
            cell.customCollectionViewController = self
            return cell
        }
    }

我知道你在这里做什么,但我不是使用UICollectionViewConroller,而是使用几个UICollectionViews。在我的情况下,这将如何工作?是的,只要类型正确,并且您将弱引用设置为正确的collectionView;弱引用适用于任何引用类型。我希望此链接将对您有所帮助。