【问题标题】:UICollectionView didSelectRowAt is never calledUICollectionView didSelectRowAt 永远不会被调用
【发布时间】:2019-06-25 21:34:55
【问题描述】:

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

我在滚动视图中有一个视图,当用户点击屏幕退出键盘时,我将其子类化为 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")
        }

    }

另外值得一提的是,集合视图正在显示数据,所以我知道至少 cellForItemAt 函数正在触发,它只有 didSelectItemAt 未被调用。

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

【问题讨论】:

  • 所以你有一个包含全屏集合视图的视图,没有其他滚动视图,任何地方都没有点击手势识别器?没有子视图添加到集合视图?
  • 我自己写的。
  • @AkshayRaina 在情节提要中,请检查 User Interaction Enabled 是否为 UICollectionViewUICollectionViewCell 打勾。
  • 不,实际上我有一个带有注册表单的滚动视图,最后我有一个可水平滚动的集合视图,任何地方都没有点击手势识别器,并且集合视图单元格内部有一个 textView
  • @AkshayRaina 您没有获得点击,因为点击是针对 TextView。如果您需要对 didSelect 进行操作,则禁用单元格内的 TextView 的用户交互

标签: ios swift xcode uicollectionview


【解决方案1】:

如果有人有同样的问题,这里是修复, 正如 Parth 建议的那样,我没有得到点击,因为点击是针对 TextView 的。我必须禁用单元格内的 TextView 的用户交互才能注册单元格上的点击。

【讨论】:

    【解决方案2】:

    所以UICollectionViewUITableView 都继承自UIScrollView,不应该放在另一个scrollView 中。苹果认为这是一个坏主意,会引起问题。所以最好的答案是不要那样做。

    通常,如果您需要这样做,通常最好将UICollectionView 嵌入UITableViewCell 中以创建您的布局。在不确切知道您要做什么的情况下,很难提出最佳方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-27
      • 2013-10-12
      • 2012-03-27
      • 2013-08-29
      • 2013-11-03
      • 1970-01-01
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多