Ios tvOS:在UITableView中实现UICollectionView时,无法滚动UICollectionView项
我已经实现了这一点:Ios tvOS:在UITableView中实现UICollectionView时,无法滚动UICollectionView项,ios,swift,uitableview,uicollectionview,tvos,Ios,Swift,Uitableview,Uicollectionview,Tvos,我已经实现了这一点: 按此操作。 问题是: 我无法滚动浏览收藏项目。 我认为这与以下事实有关:我所遵循的项目是针对iOS的,而我的项目是针对tvOS的。 我发现了一个有点类似的例子。一个链接到这个GitHub的答案,它的实现似乎与我的没有什么不同 以下是相关代码: ViewController.swift tableViewCell.swift 在另一个项目中,我独立地实现了collectionView。即:它没有嵌入到tableView中。而且它工作得很好 我从突出显示所选单元格的项目中复
按此操作。
问题是:
- 我无法滚动浏览收藏项目。
我发现了一个有点类似的例子。一个链接到这个GitHub的答案,它的实现似乎与我的没有什么不同 以下是相关代码: ViewController.swift tableViewCell.swift 在另一个项目中,我独立地实现了collectionView。即:它没有嵌入到tableView中。而且它工作得很好 我从突出显示所选单元格的项目中复制了代码,并将其添加到
tableViewCell.swift
中,但没有任何影响
所以我的问题是:
- 为什么我无法选择单元格和/或滚动屏幕 collectionView?
- 找到了答案:
通过拒绝表单元格的焦点,焦点引擎将
在屏幕上自动查找下一个可聚焦视图。在你的
大小写,即集合视图的单元格
嘿,这真的很有效。很久以来,我一直在为这件事伤脑筋。谢谢你能告诉我你是如何找到这个解决方案的吗?这样做,行就不会突出显示。这方面的工作是什么?当我滚动tableview的不同部分时,我希望它也被高亮显示。我发布了答案的链接。我不确定您是否可以突出显示tableViewCell并保持这种行为。它们是相互排斥的。@AG_HIHI感谢您在完成工作后更新您的答案。你节省了时间。我也被卡住了。很高兴我帮了忙:D
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
}
}
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)
}
}
}
func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
return false
}