[英]UICollectionViewCell detect tap
I have UICollectionView
in ViewController
class and I also have a class for UICollectionViewCell
.我在ViewController
类中有UICollectionView
,我也有一个UICollectionViewCell
类。
I have some cells and I need to detect which cell was tapped.我有一些细胞,我需要检测哪个细胞被窃听。 How can I detect that?我怎么能检测到呢?
Use this delegate method使用这个委托方法
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
let tappedCell = collectionView.cellForItem(at:indexPath) as! CustomCellClass
print(tappedCell.tag)
}
// //
collectionView.delegate = self
// //
class CustomVC:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource { --- }
In Swift 5 you can use the didSelectItemAt method:在 Swift 5 中,您可以使用 didSelectItemAt 方法:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath) //will return [0, 0] for the first cell, [0, 1] for the second cell etc...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.