UICollectionViewCell检测点击

时间:2018-07-22 21:43:57

标签: ios swift uicollectionview

我在UICollectionView类中有ViewController,而且我也有UICollectionViewCell的类。

我有一些牢房,我需要检测一下哪个牢房被窃听。我该如何检测?

2 个答案:

答案 0 :(得分:2)

使用此委托方法

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 { --- }

答案 1 :(得分:0)

在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...
}
相关问题