kangarooChris
kangarooChris

Reputation: 768

didSelectItemAt indexPath: is not working in UICollectionview for some reason

Here is my code for the collectionView. All is working perfect except when I click on an item in the collectionView the "didSelectItemAt" function does not work (print statement is not executed). I don't expect you to know why but can you please tell me what I could possibly check to get the select function going.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell
    let key = imageArray[indexPath.row]
    cell.displayContent(image: key)  ///function in CollectionViewCell
    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("in here\(indexPath.row)")
}

Upvotes: 0

Views: 863

Answers (1)

dniswhite
dniswhite

Reputation: 176

I had a similar problem and realized I was missing the following:

self.collectionView.delegate = self

I build my UI programmatically so hopefully this helps you as well.

Upvotes: 2

Related Questions