5.9. Providing Contextual Menus on Collection View Cells
You want to provide a menu to the user when she long-presses an item in your collection view. Through this menu, she may then be able to copy an item, move an item, etc.
Contextual menus are built into collection views by default. To
enable them, all you have to do is implement the following methods from
The runtime passes the method an index path to an item. The method returns a Boolean value indicating whether you want the collection view to display the contextual menu for that item or not.
The runtime passes the method a selector of type
SEL. You can check the selector (usually by converting it to a string and then comparing it to a string representing the action) and find out whether you want to allow that action to happen. Return
YESto allow the action to happen and
NOto suppress it. Remember that you can always convert a selector to a string using the
NSStringFromSelectormethod. A typical selector could be
paste:for the copy and the paste contextual menu items.
Here you will perform the action that you allowed the collection view to display to the user through earlier delegate methods.
Without waiting around, we are going to extend the code that we wrote in Recipe 5.5 and allow a ...