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?
An object that manages an ordered collection of data items and presents them using customizable layouts.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With