簡體   English   中英

CollectionView didSelectItemAt 沒有被調用

[英]CollectionView didSelectItemAt is not getting called

回購: https : //github.com/Metaxa007/RunPersonalRecord

我有一個 collectionView 並想通過單擊一個項目來執行 segue。 問題是,當我點擊一個項目時,didSelectItemAt 沒有被調用。 我嘗試了不同的模擬器和真實設備。 雖然長按contextMenuConfigurationForItemAt工作正常。

class RecordsCollectionViewController: UICollectionViewController {
  override func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
      let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil){ action in
          let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash.fill"), identifier: nil,discoverabilityTitle: nil, attributes: .destructive, handler: {action in
              self.deleteItem(index: indexPath.item)
          })
          
          return UIMenu(title: "", image: nil, identifier: nil, children: [delete])
      }
      
      return configuration
  }
  
  // MARK: UICollectionViewDataSource
  
  override func numberOfSections(in collectionView: UICollectionView) -> Int {
      
      return 1
  }

  
  override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      
      return distancesArray.count
  }
  
  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as? RecordsCollectionViewCell {
          cell.setupCell(distance: distancesArray[indexPath.item])
          
          return cell
      }
      
      return UICollectionViewCell()
  }
  
  override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      print("not getting called")
      performSegue(withIdentifier: showRecordsTVCsegue, sender: self)
  }
}

單元格看起來像這樣:

class RecordsCollectionViewCell: UICollectionViewCell {
    
    @IBOutlet weak var distanceTextView: UILabel!
    
    let formatter = NumberFormatter()
    
    func setupCell(distance: Int32) {
        let formatter = NumberFormatter()
        
        formatter.minimumFractionDigits = 0
        formatter.maximumFractionDigits = 2
        
        if distance >= 1000 {
            self.distanceTextView.text = "\(formatter.string(from: NSNumber(value: Double(distance) / 1000)) ?? "") km"
        } else {
            self.distanceTextView.text = "\(distance) m"
        }
        
        self.layer.cornerRadius = 25
        self.clipsToBounds = true
    }
    
}

我在 SO 上嘗試了不同的解決方案,但沒有一個對我有幫助。 例如

        collectionView.allowsSelection = true
        collectionView.isUserInteractionEnabled = true

而且我也不使用手勢識別器。

檢查了您的回購並找到了解決方案。

轉到 xib 中的 recordsCell -> ContentView 並刪除自定義類RecordsCollectionViewCell就像屏幕截圖一樣。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM