Enrique Enrique - 1 year ago 190
Swift Question

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

the method
is not called.

My views structure is:

Views structure

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

override func viewWillAppear(animated: Bool) {

override func viewDidLoad() {


// Do any additional setup after loading the view.

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

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



The container view above has the following structure:

enter image description here

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.textAlignment = .Center

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.


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

enter image description here

Answer Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download