Why is my UICollectionView cell not clickable in my swift ios app?
Asked Answered
L

6

6

I'm using UICollectionView in my swift class, it's placed on my UIViewController. I connected the collectionView to the outlet in my code, I set up delegate and datasource and I see the outcome in my app. Everything works besides the fact that when I click each cell - nothing happens.

My code is as follows:

class UsersList: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


@IBOutlet weak var tview: UICollectionView!

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    tview.backgroundColor = UIColor.whiteColor() //this works
    tview.delegate = self
    tview.dataSource = self
}

func collectionView(tview: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("You selected cell #\(indexPath.item)!")
    //this does not appear in the console :(
}

Is there anything else I could do to see the print msg in the console?

Lambkin answered 7/4, 2016 at 21:20 Comment(0)
F
4

In swift, the parameter name and function name identify a function together. UICollectionViewDelegate have function

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

but not

func collectionView(tview: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

Fey answered 8/4, 2016 at 3:6 Comment(2)
I tried that but it didn't help, I'm still not able to display any message while clicking cells... I thought it might be caused by the fact that I didn't put the override word before the func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) but then when I did that I got a message that Method does not override any method from its superclass... Is there anything else I might miss?Lambkin
UICollectionViewDelegate is a protocol, there is no need to add override keywords. You can check the uicollectionView's allowsSelection , it should be true.Fey
D
3

Try removing all of your UIGestures from the view then test to see if the UICollectionView Cells are able to be interacted with normally.

In my case, I had to remove the lines:

let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    self.view.addGestureRecognizer(tap)

For some reason this was getting in the way of being able to click on my cells.

Defoe answered 6/1, 2019 at 19:38 Comment(0)
O
1

For me, I had to set the UICollectionView's delegate. For example, in viewDidLoad():

collectionView.delegate = self

This is necessary or the delegate methods will not be called!

Orphaorphan answered 10/4, 2020 at 4:47 Comment(1)
Hi Chloe! Can you please add an explanation as to why that was the solution? Welcome to SO!Meteoroid
C
0

Do make sure the user interaction is enabled in storyboard

Concurrence answered 8/4, 2016 at 2:34 Comment(2)
It is enabled, so it must be something else :(Lambkin
another possible issue is you set the tview.delegate = self, tview.dataSource = self in viewWillAppear, please do this in viewdidload! Hope it helpConcurrence
R
0

I had to change scroll direction from vertical to horizontal in storyboard and it worked for me.

Randeerandel answered 3/1, 2020 at 11:29 Comment(0)
G
0

My problem was that isUserInteractionEnabled was false for the parent view of the collectionView. I had taken the code for the parent view from somewhere else where evidently that had been the right thing to do. But not here... Sigh...

Gault answered 23/5, 2021 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.