gpt4 book ai didi

ios - UICollectionView 中的按钮不接收触摸事件

转载 作者:行者123 更新时间:2023-11-28 06:18:52 25 4
gpt4 key购买 nike

我已经四处寻找了一段时间,但似乎无法弄清楚如何阻止 Collection View 单元格使用触摸事件。

我需要将触摸事件向下传递到相应的单元格中,以便可以按下单元格中的按钮。我在想我可能需要弄清楚如何禁用 UICollectionView 的 didSelectCellAtIndexFunction?

我也认为这是一个潜在的解决方案:collectionView.cancelsTouchesInView = false

此外,此链接可能有助于有人回答我的问题:How to add tap gesture to UICollectionView , while maintaining cell selection?

提前致谢!

编辑:

我还应该补充一点:我的按钮被添加到一个 View 中,该 View 又被添加到单元格的 contentView 中。我的代码全部以编程方式完成,因此我根本没有使用界面生成器。

最佳答案

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return false // all cell items you do not want to be selectable
}

假设所有按钮都连接到同一个选择器,您需要一种方法来区分单击了哪个单元格的按钮。找出按钮的单元格索引的方法之一是:

func buttonPressed(button: UIButton) {
let touchPoint = collectionView.convertPoint(.zero, fromView: button)
if let indexPath = collectionView.indexPathForItemAtPoint(touchPoint) {
// now you know indexPath. You can get data or cell from here.
}
}

关于ios - UICollectionView 中的按钮不接收触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283792/

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