samedi 25 avril 2015

iOS swift custom UICollectionView with Custom Cell does not call didSelectItem


I got some issues with the following code: I created a custom Cell with a Custom UICollectionView class. I used Xib files instead of the storyboard. It never calls the didSelectItemAtIndexPath method but why? I am really desperate :'(.

    import UIKit

class CustomCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    var refreshControl : UIRefreshControl = UIRefreshControl()

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.delegate = self
        self.dataSource = self

        var flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
        self.collectionViewLayout = flowLayout

        self.refreshControl.addTarget(self, action: "refreshData:", forControlEvents: UIControlEvents.ValueChanged)
        self.addSubview(refreshControl)

        self.userInteractionEnabled = true


        self.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "outfit")
        self.registerNib(UINib(nibName: layoutHelper.getCellXibName("CustomCollectionViewCell"), bundle: nil), forCellWithReuseIdentifier: "outfit")

        self.backgroundColor = UIColor.whiteColor()

    }

    //
    // MARK: UICollectionViewDataSource - Methods
    //

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        var cell : CustomCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("outfit", forIndexPath: indexPath) as! CustomCollectionViewCell
        cell.label.text = "Outfit #" + String(indexPath.row)
        let url = NSURL(string: "http://ift.tt/1DvKe1d")
        cell.imageView.sd_setImageWithURL(url, completed: nil)
        return cell
    }


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        println("at index path %i", indexPath.row)
}


Aucun commentaire:

Enregistrer un commentaire