簡體   English   中英

當自定義collectionViewCell中有標簽時,didSelectItemAtIndexPath不被調用

[英]didSelectItemAtIndexPath does not get called when a custom collectionViewCell has label in it

我有一個自定義的collectionViewCell,其標簽覆蓋了整個collectionViewCell的大小。 一切似乎都按計划進行,但唯一似乎不起作用的是collectionViewCell不會調用didSelectItemAtIndexPath 我做錯了什么? UILabel為什么不將事件傳遞給超級視圖。 在UILabel上啟用了用戶交互。

編輯:將標簽手勢添加到標簽

  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    collectionView.registerNib(UINib(nibName: "ProductSizeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! ProductSizeCollectionViewCell
    let tap = UITapGestureRecognizer()
    tap.addTarget(self, action: #selector(ProductDetailPageController.sizeTapped(_:)))
    cell.sizeLabel.tag = indexPath.row
    cell.sizeLabel.addGestureRecognizer(tap)
    return cell
 }


func sizeTapped(sender : UITapGestureRecognizer) {
    let view  = sender.view as? UILabel
    print(view!.tag)
    let indexPath = NSIndexPath(forItem: view!.tag, inSection: 0)
    let cell = collectionViewSize.cellForItemAtIndexPath(indexPath) as! ProductSizeCollectionViewCell
    if !cell.selectedCell {
        cell.selectedCell = true
        cell.layer.borderColor = UIColor(hexString: "9E9E9E").CGColor
    } else {
        cell.selectedCell = false
        cell.layer.borderColor = UIColor(hexString: "E8E6E4").CGColor
    }
    collectionViewSize.reloadData()
}

可能您在其他地方遇到了問題。 您是否可以通過在控制台上進行登錄來仔細檢查是否真的設置了集合視圖委托?

也:

collectionView.registerNib(UINib(nibName: "ProductSizeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell")

應該以某種設置方法設置一次。

因為集合視圖(作為表視圖)使用可重復使用的單元格,所以此行:

cell.sizeLabel.addGestureRecognizer(tap)

可以將多個手勢識別器添加到一個標簽。 在那種情況下,我更喜歡由單元觸發的回調塊。

暫無
暫無

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

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