1
我有一個自定義collectionViewCell,標籤覆蓋整個collectionViewCell大小。一切似乎按計劃進行,但唯一看起來不起作用的是collectionViewCell不調用didSelectItemAtIndexPath
。我做錯了什麼?爲什麼不是UILabel將事件傳遞給超級視圖。用戶交互在UILabel上啓用。didSelectItemAtIndexPath不會在自定義collectionViewCell中有標籤時調用
編輯:添加TAP姿態LABEL
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()
}
然後添加你的標籤的手勢,並獲得手勢動作 –
是的,我已經這樣做了,但是在選擇一個單元格時,選擇器似乎被多次觸發。 – MrDank
你可以顯示一些代碼 –