ios - UICollectionView didSelectRowAt 从未被调用

标签 ios swift xcode uicollectionview

我正在尝试在我的 UICollectionView 中选择一个单元格,我已经完成了所有工作,包括设置委托(delegate)和数据源,我的 ScrollView 上没有点击手势识别器。

我在 ScrollView 中有一个 View ,我将其子类化为 UIControl 以在用户点击屏幕退出键盘时处理,但我也删除了它并且 didSelectItemAt 仍然没有触发。 这是我的代码:

@IBOutlet weak var templatesCollection: UICollectionView!

   override func viewDidLoad() {
        super.viewDidLoad()
        templatesCollection.delegate = self
        templatesCollection.dataSource = self
    }

    extension AddCardViewController : UICollectionViewDelegate,UICollectionViewDataSource
{


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


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = templatesCollection.dequeueReusableCell(withReuseIdentifier: "template", for: indexPath) as! TemplateCollectionViewCell
        cell.templateView.text = templates[indexPath.row]
        cell.alignTextVertically()
        cell.isUserInteractionEnabled = true
        cell.layer.shadowColor = UIColor.black.cgColor
        cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)
        cell.layer.shadowRadius = 2.0
        cell.layer.shadowOpacity = 0.5
        cell.layer.masksToBounds = false

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("shfgdsfgidks")
    }

        func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            print("Deselected")
        }

    }

此外,值得一提的是, Collection View 正在显示数据,因此我知道 cellForItemAt 函数至少正在触发,只有 didSelectItemAt 没有被调用。

为 collectionView 及其单元启用了用户交互。 另外,这是我的层次结构的照片

enter image description here

最佳答案

所以 UICollectionViewUITableView 都继承自 UIScrollView 并且不应该放在另一个 scrollView 中。 Apple 表示这是一个坏主意,会导致问题。所以最好的答案是不要那样做。

通常,如果您需要这样做,通常最好在 UITableViewCell 中嵌入一个 UICollectionView 来创建您的布局。不知道你到底想做什么,很难提出最好的方法

如果您必须这样做,那么您需要查看UIGestureRecogniserDelegate并配置他们可以同时识别的特定手势。

关于ios - UICollectionView didSelectRowAt 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54477272/

相关文章:

ios - 在 iPhone 上运行 React Native 应用程序

iOS Per-App VPN 未连接到互联网

ios - 我们可以获得测试卡来在 BrainTree SDK 中测试 ApplePay 吗?

ios - 将渐变效果应用于模糊 View

ios - 为什么我在 Xcode 7.3 中找不到 iPhone SE 模拟器?

iOS应用程序有多个主线程

ios - Swift 3 使用 NSAttributedString 渲染 textview 但想要更改默认字体

json - swift 4 : how to implement codable protocol for json array of different types

ios - 使用扩展创建自定义 UIButton

ios - 如何将pod添加到本地pod项目