Search code examples
iosios6uicollectionviewuicollectionviewcell

UICollectionViewCells and Buttons


I've been trying to get my UICollectionView to respond differently to single and double taps but all the answers I have found seem to suggest this is not really feasible because the single taps get recognised first. It works on really slow taps, but anything faster always initiates the default gesture recogniser (if anybody has got this to work I would love to know)...

So anyway, I have now resorted to putting buttons in my UICollectionViewCell (which has it's own class and NIB file).

The question is this:

What is considered the best way to use the button in the UIViewController of the collectionView?

I currently have a protocol in the header of my subclass of UICollectionViewCell and have declared my viewController as the delegate and implemented the required protocol functions.

In collectionView:cellForItemAtIndexPath: I set the VC as the delegate of the cell.

It all works but it seems a bit long-winded, and maybe not a great way of doing this.

The other way I was thinking of was instead of using delegates to simply call addTarget:Action: on the property of the UICollectionViewCell in collectionView:cellForItemAtIndexPath:.

This seems simpler but the delegate pattern looks to me like the better fit.

Any and all advice on which would be better, why, and any more appropriate alternatives welcomed!

Thanks.


Solution

  • You're doing the right think using the delegation pattern. The ultimate responsible object for any action of your views is the viewController who's displaying those views. Therefore, using it as the delegate for you cell's protocol is just right.