The example is for nesting a smallCollectionVIew into a BigCollectionViewCell, whose collection view is constructed in BigCollectionViewController. Going to manipulate bigCollectionView in one VC through clicking cell of smallCollectionView in another class.
1. Delegate definition
Where: BigCollectionViewCell.swift (which file you want to call a delegate function)
protocol BigCategoryCellDelegate :class {
func didSelectSmallCell (#indexPath: NSIndexPath)
}
2. Create delegate property
Where: BigCollectionViewCell.swift (a property of BigCollectionViewCell class)
weak var delegate: BigCategoryCellDelegate?
3. Function call
Where: BigCollectionViewCell.swift (collectionView = smallCollectionVIew)
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.delegate?.didSelectSmallCell(indexPath: indexPath)
}
-----------------------------------------s-e-p-e-r-a-t-o-r----------------------------------------------
4. Inherit the delegate
where: BigCollectionViewController.swift (whose property you want to manipulate)
class BigCollectionViewController: UIViewController, ..., BigCategoryCellDelegate {}
5. Link the delegate
where: BigCollectionViewController.swift (right after the BigCollectionViewCell() is dequeued)
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("bigCollectionViewCell", forIndexPath: indexPath) as? BigCollectionViewCell
cell?.delegate = self
6. Function Definition
where: BigCollectionViewController.swift
func didSelectSmallCell (#indexPath: NSIndexPath){
...
bigCollectionView?.reloadData()
}