【问题标题】:tvOS: Unable to scroll through UICollectionView items when it is implemented like this: UICollectionView inside UITableViewtvOS:当它像这样实现时无法滚动 UICollectionView 项目:UITableView 内的 UICollectionView
【发布时间】:2020-09-21 10:25:05
【问题描述】:

我已经实现了这个:
关注此tutorial
问题出在:

  • 我无法滚动浏览收藏项目。

我认为这与我关注的项目是针对 iOS 而我的项目是针对 tvOS 的事实有关。
我发现了一个有点相似的question。链接到此 GitHub Repo 的答案似乎与我的实现没有什么不同。

以下是相关代码:

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

在一个单独的项目中,我已经独立实现了一个collectionView。即:它没有嵌入到 tableView 中。而且它工作得很好

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

所以我的问题是:

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

【问题讨论】:

    标签: ios swift uitableview uicollectionview tvos


    【解决方案1】:

    找到答案here

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

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

    【讨论】:

    • 嘿,这真的有效。很长一段时间以来,我一直对此感到头疼。谢谢。你能告诉我你是如何找到这个解决方案的吗?
    • 通过这样做,行不会被突出显示。有什么办法解决这个问题?当我滚动 tableview 的不同部分时,我希望它也被突出显示。
    • 我发布了答案的链接。我不确定您是否可以同时突出显示 tableViewCell 并保持这种行为。它们是相互排斥的。
    • @AG_HIHI 感谢您在完成工作后更新您的答案。你节省了时间。我也被卡住了。
    • 很高兴我能帮上忙:D
    猜你喜欢
    • 2019-09-28
    • 2020-09-17
    • 2018-11-02
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多