我为我的 tvOS 应用程序制作了某种 Netflix 布局,其中包含多个名为:featuredCollectionView
和 standardCollectionView
的 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/