Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 531
user3766930 Avatar asked Apr 07 '16 21:04

user3766930


People also ask

What is swift UICollectionView?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is the use of datasource and delegate in UICollectionView in IOS?

This method asks the data source object to provide a supplementary view to display in the collection view. It asks the datasource object whether the specified item can be moved to another location in the collectionview. This method moves the specified item to the specified indexpath.

What is collection reusable view?

Overview. Reusable views are so named because the collection view places them on a reuse queue rather than deleting them when they're scrolled out of the visible bounds. Such a view can then be retrieved and repurposed for a different set of content.


2 Answers

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)

like image 118
Jeff Avatar answered Sep 28 '22 03:09

Jeff


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.

like image 43
drfalcoew Avatar answered Sep 28 '22 03:09

drfalcoew