【问题标题】:Why UICollectionView didSelect method does not work?为什么 UICollectionView didSelect 方法不起作用?
【发布时间】:2016-08-19 10:01:26
【问题描述】:

我以编程方式创建了我的UICollectionView,在这种情况下我的didSelectItemAtIndexPath 方法根本不会调用。

let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.view.frame), height: 360), collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.userInteractionEnabled = true

那么,有什么问题吗?为什么当我点击单元格时没有得到响应?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Selected Cell: \(indexPath.row)")
}

【问题讨论】:

  • 委托对象不是nil?
  • 1) 尝试设置Background ColorUICollectionViewUICollectionViewCell。看看它是否在屏幕上正确可见。 2) 在UICollectionView 上设置userInteractionEnabled = true
  • @pkc456 为什么?我已经看到了我的单元格,并且已经启用了 userInteraction
  • @Doe 这个测试练习只是交叉检查 UI 元素的位置。有时 UI 组件会被其他视图覆盖(由于错误的自动布局)。或者有时 UI 元素的框架在外部但在屏幕上可见,因此无法获得点击操作。
  • 也许你在 vc 的视图中添加了一些手势识别器?

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

我知道这可能会迟到,但出于功能目的,CollectionView[didSelectItem] 未响应触摸可能是因为您还启用了addGestureRecognizer,因此请检查您是否在您的@987654323 上设置了手势识别器@喜欢这样collectionView?.addGestureRecognizer(tapGestRecognizer)

【讨论】:

  • 谢谢。这节省了我的时间。我在我的表格视图上启用了手势识别器,其单元格包含 collectionView,它阻止了代表被触发
  • 谢谢。就我而言,错误是我在视图控制器上添加了手势识别器。
【解决方案2】:

我刚遇到这种情况。

您是否在集合视图单元格中放置了按钮或交互式视图? 您的触摸由它处理,然后不调用 didSelect。

通过 Interface builder 关闭 User Interaction Enabled 属性,或以编程方式设置为 false。

【讨论】:

  • 谢谢你让我开心
【解决方案3】:

签入UiCollectionviewCell 子类isUserInteractionEnabled 属性或以编程方式将其设置为true

self.contentView.isUserInteractionEnabled = true

【讨论】:

    【解决方案4】:

    请确保您没有将 UICollectionViewDelegate 和 UICollectionViewDelegateFlowLayout 放在一起。

    【讨论】:

      【解决方案5】:

      在 Project Navigator 中选择 ViewController.swift。用这个更新类声明:

      class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource 
      
      
      //After class declaration create a new variable:
      
       var collectionView: UICollectionView!
      
      
      
        override func viewDidLoad() {
         super.viewDidLoad()
           // Do any additional setup after loading the view, typically from a nib.
         let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
         layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
         layout.itemSize = CGSize(width: 90, height: 120)
      
         collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
         collectionView.dataSource = self
         collectionView.delegate = self
         collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
         collectionView.backgroundColor = UIColor.whiteColor()
         self.view.addSubview(collectionView)
      }
      
      
      
      func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
          print("Selected Cell: \(indexPath.row)")
      }
      

      【讨论】:

        【解决方案6】:

        我发现了一个奇怪的边缘情况,我不小心为单元格的容器视图指定了一个类(以及一个对容器视图无效的类),一切看起来都很好,我没有收到任何警告,但是 @987654321 @ 不会被调用。

        这是我生命中的 5 个小时,我想回来。

        【讨论】:

          猜你喜欢
          • 2013-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-03
          • 1970-01-01
          • 2013-01-22
          • 2018-08-06
          • 2012-01-14
          相关资源
          最近更新 更多