问题描述
- 我无法滚动查看收集项目。
我认为这与以下事实有关:我关注的项目适用于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
中,但是它根本没有影响。
所以我的问题是:
- 为什么我无法选择一个单元格和/或滚动浏览 collectionView?
解决方法
找到答案here:
通过拒绝表格单元格的焦点,Focus引擎将 自动在屏幕上找到下一个可聚焦的视图。在你的 情况就是集合视图的单元格。
func tableView(_ tableView: UITableView,canFocusRowAt indexPath: IndexPath) -> Bool {
return false
}