Reputation: 1742
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:
Upvotes: 0
Views: 3327
Reputation: 821
collectionView.delegate = self
collectionView.dataSource = self
set delegate and datasource UIViewController class and not in storyboard
Upvotes: 0
Reputation: 1742
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!
Upvotes: -1
Reputation: 5747
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
Upvotes: 2