I recently came up with this question, and I have a way to demonstrate Akhilrajtr's comment, as this may help others as well.
First in your cell class you need the protocol at the top of the file:
protocol YourCellDelegate: NSObjectProtocol{ func didPressCell(sender: Any) }
Then in you cell class variables add this:
var delegate:YourCellDelegate!
When you perform an action inside your cell, activate the function of your protocol:
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { delegate.didPressCell(sender: indexPath) }
On your super primary controller, you must match the delegate you are creating:
class MyViewController: UIViewController, YourCellDelegate{...}
And, implement a protocol function that will fire when you click on a cell, just like you defined it before:
func didPressCell(sender: Any){ let vc = SomeViewController() self.navigationController?.pushViewController(vc, animated: true) }
Of course, do not forget to give a link for your delegate regarding the creation of a cell instance in the cellForItem function:
cell.delegate = self
source share