Swift: Click onto cell of UICollectionView and open AlertViewController
Asked Answered
N

3

7

In my app, I am using an UICollectionView. Now I want to develop an UIAlertController, when clicking onto any cell of the collection view. I started with following code:

extension HomeViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    …
}

// specify cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    ….
}

// called when widget is moved
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        …
}

// called when clicked
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    print("Got clicked!")
}

}

But somehow, "Got clicked!" is never printed.

Nim answered 28/10, 2017 at 8:53 Comment(3)
Did you set the delegate and the data source?Hogan
No, how do I do that? Sorry, I am a beginner :DNim
@AlexanderJeitler-Stehr, It's great to know that you've started iOS and learning Swift. You just miss adding UICollectionViewDelegate to the extension. Just add it after UICollectionViewDataSource and you're good to go. Make sure you've bind the delegate to the HomeViewController . Happy Coding :)Stellite
P
17

try next:

extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate {

}

or

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   ...
   cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(_:))))
}

func tap(_ sender: UITapGestureRecognizer) {

   let location = sender.location(in: self.collectionView)
   let indexPath = self.collectionView.indexPathForItem(at: location)

   if let index = indexPath {      
      print("Got clicked on index: \(index)!")
   }         
}
Punt answered 28/10, 2017 at 9:18 Comment(4)
Thanks for your answer :) I will now use the second version. Could you please tell me how to pass the index of the tapped cell from the tap function to the func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell ?Nim
You don't need do it. You add gesture recognizer in your each cell, and when you select one of cells, in method tap(_:) you obtain index. If you want change your selected cell, you can use this code to use this cell: let cell = self.messageCollectionView.cellForItem(at: index)Punt
Thanks, that did it!Nim
you just saved my ass! :)Stewardson
B
3

This is the version using the delegate:

extension HomeViewController: UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("item at \(indexPath.section)/\(indexPath.item) tapped")
  }
}

Instead of using the extension you can also just add UICollectionViewDelegate and the collectionView(...didSelectItemAt:...) function to the class HomeViewController directly:

class HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print("item at \(indexPath.section)/\(indexPath.item) tapped")
  }
}
Bashful answered 2/1, 2020 at 21:57 Comment(0)
A
0

This is because you might have placed some UIButton in the cell. Tap on some empty area in the cell then you will get the 'Got click on index'

Arbitral answered 19/7, 2020 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.