gpt4 book ai didi

Swift:永远不会调用 Collection View 单元格中 UIControl 的 tvOS IBAction

转载 作者:行者123 更新时间:2023-11-28 05:40:33 25 4
gpt4 key购买 nike

我知道这个问题已经被问过很多次了,但我仍在学习,我已经在 S.O. 中尝试了所有可能的解决方案。而且我一次都没有运气。所以这是问题所在:

  • 我正在使用 Xcode 11 beta 3(希望这不是问题所在!)
  • 我在 View Controller 中有一个简单的 Collection View
  • 我在单元格内放了一个 tvOS' TVPosterView - 它继承自 UIControl 并自行测试它的行为确实像一个按钮(IBAction 被调用)。
  • collection view 有推送海报的动画
  • 海报 View 有一个默认图像,我只是更改标题
  • 我将一个 IBAction 从海报 View 拖到 IB 中的单元格类
  • 除了 IBAction, Collection View 可以正常显示和滚动,更改海报的标题
  • 如果我将 Collection View 委托(delegate)给 View Controller ,collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 会被调用。

代码如下:

View Controller

import UIKit
import TVUIKit

class SecondViewController: UIViewController {

@IBOutlet weak var myView: UIView!
@IBOutlet weak var myCollection: MyCollection!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

extension SecondViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! MyCell

cell.myPoster.title! = "Header " + String(indexPath.row + 1)

cell.myPoster.tag = indexPath.row + 1
cell.posterTapAction = { cell in
print("Header is: \(cell.myPoster.title!)")
}

return cell
}
}

extension SecondViewController: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// This one works - but that's not what I am looking for
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}

Collection View

import UIKit

class MyCollection: UICollectionView {

override func awakeFromNib() {
super.awakeFromNib()
}

override func layoutSubviews() {
super.layoutSubviews()
}
}

细胞

import UIKit
import TVUIKit

class MyCell: UICollectionViewCell {

var posterTapAction: ((MyCell) -> Void)?

@IBOutlet weak var myPoster: TVPosterView!
@IBAction func myAction(_ sender: TVPosterView) {
posterTapAction?(self)
print("Poster pressed \(sender.tag)")
}
}

知道我遗漏了什么吗?我很乐意在按下海报后设法打印一个虚拟字符串。

我也尝试过选择器和委托(delegate)的解决方案,但都没有用。因此,请让我们专注于 closures 的这个特定解决方案。 .谢谢!

最佳答案

我终于找到了。像往常一样,这是由于 tvOS 焦点。基本上,单元格必须不可聚焦,以便单元格内的 UIControl 元素(在我的例子中是 TVPosterView,但它可以是任何 UIControl)将成为焦点。这很诡异,因为在图形上它确实看起来好像海报有焦点(可以稍微旋转海报)。

解决方案是添加collectionView(_:canFocusItemAt:)UICollectionViewDelegate

extension SecondViewController: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView,
canFocusItemAt indexPath: IndexPath) -> Bool {
return false
}

func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
// Now, thanks to the function above, this is disabled
print("From didSelectItemAt indexPath: \(indexPath.item + 1)")
}
}

我已经在这个问题的代码中进行了测试,并将其也添加到我的项目中,终于可以了!

关于Swift:永远不会调用 Collection View 单元格中 UIControl 的 tvOS IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com