0

i have UICollection view in a ViewController and it's not responding to didSelectItemAtIndexPath at all.

// super class
class ViewController: UIViewController, iCarouselDelegate, iCarouselDataSource, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!


// delegate
override func viewDidLoad() {
        super.viewDidLoad()

        // collection view delegate and datasource
        collectionView.delegate = self
        collectionView.dataSource = self


// did select item
        func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) {

           print(indexPath)

        }

and this delegate from IB . UiCollectionView delegate in IB

2 Answers2

3

several guesses might be helpful:

  • did you accidentally overriding - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath?

  • make sure set collectionView.userInteractionEnabled to true

  • if some high priority UIResponder like UIGestureRecognizer and UIButton added to cell or its subview, corresponding method should be call instead

Chen Wei
  • 521
  • 4
  • 10
  • i have an ICarousel in same viewController , may mb this the reason ? @Chen Wei – Abdullah Ibn Farouk Sep 23 '16 at 18:25
  • No, did you connect that '@IBOutlet collectionview' to the collection view in storyboard? If so the name in screenshot should be the same with variable "collectionview" rather than the default name "Collection View" – Chen Wei Sep 23 '16 at 23:34
  • i did connect IBOutlet collectionview @Chen Wei , and thanks at all . i solved my problem in another way .. i'm going to answer this question. – Abdullah Ibn Farouk Sep 24 '16 at 05:31
0

i had to use another way to run away from this issue and ship my project in time.

i made a button inside my cell and in this method "cellForItemAtIndexPath" and added target for it like so:

imageButton.addTarget(self, action: #selector(ViewController.imageButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

and passed the selected indexPath.row in this button tag like so :

imageButton.tag = indexPath.row

and this my Action :

func imageButtonAction(sender: UIButton) {

let categoryId = categories[sender.tag]["id"]
    let categoryName = categories[sender.tag]["category_name"]

    let mainCategory = self.storyboard?.instantiateViewControllerWithIdentifier("MainCategoryViewController") as! MainCategoryViewController
    mainCategory.mainCategoryId = categoryId!
    mainCategory.title = categoryName!
    self.navigationController!.pushViewController(mainCategory, animated: true)

}