简体   繁体   中英

didSelectItemAtIndexPath of UICollectionView is not called

After a couple of days of going crazy I wasn't able to find a solution to my issue. The problem is this: After selecting a cell in a UICollectionView the method didSelectItemAtIndexPath is not called.

My views structure is:

视图结构

This view is managed by a controller which has following methods:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
}

override func viewDidLoad() {
    super.viewDidLoad()

    initCategoriesCollection()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func initCategoriesCollection(){
    let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;
    addChildViewController(ccVC)
    ccVC.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
    containerView.addSubview(ccVC.view)

    ccVC.didMoveToParentViewController(self)

}    

The container view above has the following structure:

在此处输入图片说明

And this container view is managed by a ViewController which implements these methods:

// tell the collection view how many cells to make
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.e1Categories.count
}

// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CategoryCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.categoryName.text = self.e1Categories[indexPath.item].string
    cell.categoryName.numberOfLines = 0
    cell.categoryName.sizeToFit()
    cell.categoryName.textAlignment = .Center
    cell.categoryCircle.makeCircle()

    return cell
}

// MARK: - UICollectionViewDelegate protocol

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
}

Does anyone know what is happening? I have tried to remove ScrollView because maybe It is intercepting touch events, but it is still not working at all. After reading some other Questions here, in Stackoverflow, none of them has a solution for me.

Thanks in advance.

EDIT:

I have connected my delegate and dataSource through the Storyboard, as you can see in the picture below:

在此处输入图片说明

In your initCategoriesCollection() , you are creating a new CategoriesCollectionViewController :

let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;

According to you:

And this container view is managed by a ViewController which implements these methods:

Then you are adding the ccVC to another controller that implement the delegate func didSelectItemAtIndexPath . Therefore the delegate set on storyboard is not to the correct controller that are managing it.

You should be updating the delegate since you are adding it to another controller that is implementing it. Try update to this

let ccVC : CategoriesCollectionViewController  =  UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController;
addChildViewController(ccVC)
ccvc.collectionView.delegate = self
collectionView.delegate = self
collectionView.dataSource = self

set delegate and datasource UIViewController class and not in storyboard

First of all thanks for give me the hints to solve my problem. Following them and trying to assign the parent controller as delegate of ccvc.collectionView , It said that there wasn't any delegate in the child ViewController. My problem was that I thought that if assigned both delegate and dataSource via Storyboard It would work out, but It wouldn't.

So I decided to implement both Delegate and Datasource protocols in parent controller, remove child controller and It works like a charm now. Maybe I was misunderstanding concepts when I used the Storyboard . Thanks for your help!

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