swift - 如何使用 swift - tvOS 获取 UIcollectionview 单元格中聚焦单元格的 'parent'

标签 swift uicollectionview uicollectionviewcell tvos

我为我的 tvOS 应用程序制作了某种 Netflix 布局,其中包含多个名为:featuredCollectionViewstandardCollectionView 的 Collection View 。

我有一个包含当前焦点单元格的变量。我唯一想要的是获取所选单元格的当前 Collection View 。谁能帮我解决这个问题?

代码

func pressedThePlayPauseButton() {

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

       let collectionViewOfFocusedCell = ...

最佳答案

您始终可以在 View 层次结构中向上移动:

    var parentCollectionView = self.superview
    while parentCollectionView is UICollectionView != true {
        parentCollectionView = parentCollectionView?.superview
    }

如果需要遍历多个 Collection View ,可以将上面的 UICollectionView 改成你要找的子类。更简单的方法是在 cellForItemAtIndexPath 中为您的单元格提供对 collectionView 的弱引用;这也适用于您的 View Controller ,而不是您的 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
        }
    }

关于swift - 如何使用 swift - tvOS 获取 UIcollectionview 单元格中聚焦单元格的 'parent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700116/

相关文章:

objective-c - 如何使用 Storyboard 子类化 UICollectionViewFlowLayout

iOS:设置 UICollectionView 单元格以查看大小

html - WKWebView 在新的 View Controller 中打开

json - 无法将 Triple 类型的值调用到 Json 类型中

ios - AVPlayer 视频 currentTime 总是返回 0

ios - Collection View 网格 iOS Swift 2

swift - 如何检查 Parse 是否已初始化

ios - 在自定义 UIcollectionview 单元格的标签中显示计时器

swift - 删除单元格后 collectionViewCell 中的按钮索引错误

ios - Swift 中具有/不同单元格类型的可重用数据源