How to listen to user touches for UICollectionViewCell in iOS? How to listen to user touches for UICollectionViewCell in iOS? swift swift

How to listen to user touches for UICollectionViewCell in iOS?


You should implement UICollectionViewDelegate protocol method collectionView(_:didSelectItemAtIndexPath:). When you press one of your collection view cells this method get called. Here is sample implementation

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {    let url = thumbnailFileURLS[indexPath.item]    if UIApplication.sharedApplication().canOpenURL(url) {        UIApplication.sharedApplication().openURL(url)    }}

By the way I don't know where you get url. So I improvised a bit :)


Swift 5

didSelectItemAtIndexPath has been renamed to didSelectItemAt in Swift 5

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {         //Do your logic here    }