7

I'm currently working on a project for an iPad app. The main screen is a UICollectionView with AlbumCell's, a subclass of UICollectionViewCell. Now I wanted to add an UILongPressGestureRecognizer to pop up an UIActionSheet.

First I tried that in the UICollectionViewController, but I figured that that isn't the right place to add those. So my best guess is adding the gesture in the AlbumCell class? Then probably adding itself as delegate, so it catches it's own gestures.

Is this a good approach so far?

After I catch the gesture, I should show the UIActionSheet. Now I open it in the UICollectionViewController when the user selects a cell while in editing mode. But should I call a method on the UICollectionViewController to open it, like I do now? Or should the cell handle it's own UIActionSheet?

Eventually I got to let the UICollectionViewController what to do, might be to let him open the UIActionSheet, or handle accordingly to the result of it. How should the AlbumCell "communicate" with it?

It's something I've been struggling with multiple times, not just in this use case. Is the approach close, or should I handle those actions totally different?

Jano
  • 62,815
  • 21
  • 164
  • 192
Guido Hendriks
  • 5,706
  • 3
  • 27
  • 37
  • As AlbumCell is a view, and UICollectionViewController is a view controller. So "open the UIActionSheet" action should be managed by UICollectionViewController. You can use [Protocol](https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Protocol.html)/[Delegate](http://developer.apple.com/library/ios/#Documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html). – Kjuly Sep 21 '12 at 01:17
  • That makes sense. But the recognized should be added to the cell subclass? – Guido Hendriks Sep 21 '12 at 01:31
  • Yep, as the cell will be reused. – Kjuly Sep 21 '12 at 03:24
  • Okay. All clear so far, but how should I let the view controller know that one of the cells was pressed, and which one? – Guido Hendriks Sep 21 '12 at 09:14
  • You can create an iVar like index as a tag for your cell. – Kjuly Sep 21 '12 at 11:10
  • If I add the `UIGestureRecognizer` to the `AlbumCell`, with the delegate to `self`. I should somehow tell the `UICollectionViewController` to do something with the `Album` that belongs to the `AlbumCell`. – Guido Hendriks Sep 21 '12 at 11:16
  • Actually, i think you don't need to add the index iVar either, the pressed cell will response you action (through controller). – Kjuly Sep 21 '12 at 11:22

1 Answers1

13

You can adopt the same method of using UILongPressGestureRecognizer on UITableView.

Basically, set up one recognizer. In the selector, use the indexPathForItemAtPoint: to find out what index path the user was pressing on. Finally, if the user is pressing on a cell (and not pressing on the space in between cells), present the UIActionSheet however you like.

Community
  • 1
  • 1
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
  • Of course, why didn't I think about that myself? I knew it wasn't really the way it should be, this makes a lot more sense! Gonna implement that, pretty sure it'll do the job. :) – Guido Hendriks Sep 21 '12 at 11:50