tvOS:无法滚动 UICollectionView 项目时,它是这样实现的:UITableView 内的 UICollectionView [英] tvOS: Unable to scroll through UICollectionView items when it is implemented like this: UICollectionView inside UITableView

查看:25
本文介绍了tvOS:无法滚动 UICollectionView 项目时,它是这样实现的:UITableView 内的 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了这个:
遵循此教程.
问题来了:

  • 我无法滚动浏览收藏项.

我认为这与我关注的项目是针对 iOS 而我的项目针对 tvOS 的事实有关.
我发现了一个有点类似的问题.链接到此 GitHub Repo 的答案,其实现似乎并非如此和我的不一样.

相关代码如下:

<块引用>

ViewController.swift

class ViewController: UIViewController {@IBOutlet 弱变量 tableView:UITableView!覆盖 func viewDidLoad() {super.viewDidLoad()}}扩展视图控制器:UITableViewDelegate,UITableViewDataSource {func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回 1}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {守卫让细胞=tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as?表视图单元格别的 {致命错误(无法创建探索表视图单元格")}返回单元格}func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) ->CGFloat {返回 140}}

<块引用>

tableViewCell.swift

class TableViewCell: UITableViewCell {@IBOutlet 弱变量 collectionView:UICollectionView!覆盖 funcawakeFromNib() {super.awakeFromNib()//初始化代码collectionView.delegate = selfcollectionView.dataSource = self}覆盖 func setSelected(_ selected: Bool, 动画: Bool) {super.setSelected(选中,动画:动画)}}扩展 TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{返回 50}func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->UICollectionViewCell {let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)返回单元格}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) ->CG尺寸{//由于某种原因他选择了collectionViewCell的度量并减去了2返回 CGSize(宽:139,高:64)}//突出显示当前单元格//这不起作用func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {如果让 pindex = context.previouslyFocusedIndexPath,让 cell = collectionView.cellForItem(at: pindex) {cell.contentView.layer.borderWidth = 0.0cell.contentView.layer.shadowRadius = 0.0cell.contentView.layer.shadowOpacity = 0.0}如果让 index = context.nextFocusedIndexPath,让 cell = collectionView.cellForItem(at: index) {cell.contentView.layer.borderWidth = 8.0cell.contentView.layer.borderColor = UIColor.orange.cgColorcell.contentView.layer.shadowColor = UIColor.orange.cgColorcell.contentView.layer.shadowRadius = 10.0cell.contentView.layer.shadowOpacity = 0.9cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)collectionView.scrollToItem(at: index, at: [.centricHorizo​​ntally, .centralVertically], 动画: true)}}}

在一个单独的项目中,我独立实现了一个collectionView.即:它没有嵌入在 tableView 中.它工作正常.

我从该项目中复制了突出显示所选单元格的代码并将其添加到 tableViewCell.swift 中,但它根本没有影响.

所以我的问题是:

  • 为什么我无法选择单元格和/或滚动浏览集合视图?

解决方案

找到答案 这里:

<块引用>

通过拒绝表格单元格的焦点,焦点引擎将自动在屏幕上找到下一个可聚焦的视图.而在你case,就是集合视图的单元格.

func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) ->布尔{返回假}

I have implemented this:
By following this tutorial.
Here's the problem:

  • I am unable to scroll through the collection items.

I think this has something to do with the fact that the project I followed is for iOS and my project is for tvOS.
I've found a somewhat similar question. An answer linked to this GitHub Repo whose implementation doesn't seem that different from mine.

Here's the relevant code:

ViewController.swift

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell =
            tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TableViewCell
            else {
                fatalError("Unable to create explore table view cell")}
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140
    }
}

tableViewCell.swift

class TableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

extension TableViewCell:  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 50
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // For some reason he chose the measures of collectionViewCell and substracted 2
        return CGSize(width: 139, height: 64)
    }
    
    // Highlight the current cell
    // This doesn't work
       func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
           if let pindex  = context.previouslyFocusedIndexPath, let cell = collectionView.cellForItem(at: pindex) {
               cell.contentView.layer.borderWidth = 0.0
               cell.contentView.layer.shadowRadius = 0.0
               cell.contentView.layer.shadowOpacity = 0.0
           }
    
           if let index  = context.nextFocusedIndexPath, let cell = collectionView.cellForItem(at: index) {
               cell.contentView.layer.borderWidth = 8.0
               cell.contentView.layer.borderColor = UIColor.orange.cgColor
               cell.contentView.layer.shadowColor = UIColor.orange.cgColor
               cell.contentView.layer.shadowRadius = 10.0
               cell.contentView.layer.shadowOpacity = 0.9
               cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
               collectionView.scrollToItem(at: index, at: [.centeredHorizontally, .centeredVertically], animated: true)
           }
       }
}

In a separate project, I have implemented a collectionView independently. I.e: It was not embedded inside of a tableView. And it works just fine.

I copied the code from that project that highlights the selected cell and added it to the tableViewCell.swift but it had no impact at all.

So my question is:

  • Why am I unable to select a cell and/or scroll through the collectionView?

解决方案

Found the answer here:

By denying the focus of the table cell, the Focus engine will automatically find the next focusable view on the screen. And in your case, that is the collection view’s cell.

func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
      return false
    }

这篇关于tvOS:无法滚动 UICollectionView 项目时,它是这样实现的:UITableView 内的 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆