简体   繁体   中英

Swift UICollectionView : didSelectItemAtIndexPath not responding

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 . IB中的UiCollectionView委托

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

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)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM