-1

I'm trying to make a collection view, and everything is working except when I try to select one cell.

My method didDeselectItemAtIndexPath is not called.

In the main story board, I have a collectionviewcell controller embed in a navigation view controller. The collectionviewC calls another controller to print details, between the two of them, there is a segue called segueDetailFeature I have another project, and i did like that with a tableviewcontroller and it works.

The images are well displayed, the print in the numebrofitemsinsection returns the good value.

Here is the code of my controller:



class ViewController: UICollectionViewController {


    var listImageUrl = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // code to populate the listImageUrl

        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("nombre d'image : \(listImageUrl.count)")
        return listImageUrl.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell

        let imageView = cell.viewWithTag(1) as! UIImageView
        let urlString: NSString = listImageUrl[indexPath.row] as NSString
        let imgURL: NSURL = NSURL(string: urlString as String)!

        //Télécharger une représentation NSData de l'image à cette URL
        let imgData: NSData = NSData(contentsOfURL: imgURL)!
        imageView.image = UIImage(data: imgData)

        //imageView.image = UIImage(named: listImageUrl[indexPath.row])

        return cell
    }

    override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        print("selected item at index")
        let urlImage = listImageUrl[indexPath.row]
        imageUri = urlImage
        performSegueWithIdentifier("segueDetailFeature", sender: collectionView)
    }


}

I can't find why my code is not working, why my didDeselectItemAtIndexPath is not called. Yet, when I run it, the app use the segue and load the detail controller, but it fails because one variable hasn't been instantiated in the method did select.

Thanks in advance

Charles Truluck
  • 961
  • 1
  • 7
  • 28
La Hu
  • 85
  • 1
  • 11
  • Did you mean to implement `didSelect` rather than `didDeselect`? – matt Oct 14 '15 at 00:04
  • I think i'm dumb.. indeed i haven't seen that it was deselect, the name of their method are so long it didn't occure to me, i'll let you know it it works :-) thx ! – La Hu Oct 14 '15 at 07:34
  • I make this mistake about 20% of the time, just infrequently enough to not learn my lesson. – danh Oct 15 '15 at 01:06

1 Answers1

-1

If you take a look at this link:UICollectionView - didDeselectItemAtIndexPath not called if cell is selected You can can see that he had received the same error. His code is in Objective-C but hopefully you could probably change that to swift. He explains that the issue is that the UICollectionView does not know anything about the cell but the cell is being selected.

Community
  • 1
  • 1
Steve Sahayadarlin
  • 1,164
  • 3
  • 15
  • 32