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?

user3766930
  • 5,629
  • 10
  • 51
  • 104

6 Answers6

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)

Jeff
  • 166
  • 5
  • 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? – user3766930 Apr 08 '16 at 11:30
  • UICollectionViewDelegate is a protocol, there is no need to add `override` keywords. You can check the uicollectionView's `allowsSelection` , it should be true. – Jeff Apr 08 '16 at 11:54
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.

drfalcoew
  • 615
  • 5
  • 14
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!

huwr
  • 1,720
  • 3
  • 19
  • 34
Chloe Yan
  • 11
  • 3
0

Do make sure the user interaction is enabled in storyboard

zhenkai
  • 1
  • 2
0

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

Ruchi
  • 411
  • 4
  • 13
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...

Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32