I'm building my first application using UICollectionView
and noticed that there isn't much I can do in terms of object deletion. For UITableView
apps, there's the swipe to delete method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
When I use GMGridView
, it has behaviors similar to long press on the iPhone home screen - the view stars to shake and a delete button can be displayed, which is responsible for deleting the view. I can certainly try to replicate this behavior, but am not sure if users will "get it".
I'm interested in what are my options for letting the user delete objects from UICollectionView
- do I have to implement my own delete gestures/controls, or is there something that I'm missing (or open source)?
What I have found and am implementing for deleting and editing things in a UICollectionView are from this blog post.
Basically showing the menu for Copy/Paste ect, and adding my own actions for deletion. Its similar to iSang's answer, without adding gestures recognizers that don't work too well in UICollectionViews.
It uses the long press gesture that people have already used to bring up editing menus when wanting to copy and paste text and links in other parts of iOS.
http://paulsolt.com/2012/11/uicollectionview-custom-actions-and-uimenucontroller/
I inserted this code in my view controller that includes the CollectionView and did it this way. You are probably already doing something like this with the tap gesture to detect selected cell.
For anyone reading this in the future, here's a useful library I've been using: https://github.com/Raizlabs/RZUtils/tree/master/RZUtils/Components/RZCollectionTableView
It mimics a UITableView using a UICollectionView, so you can get the flexibility of the collection view with all of the nice editing functionality that comes with a UITableView.
If you are trying to implement a different kind of delete behavior or different layout, though, as far as I know you have to implement it from scratch. If you're new to doing custom things with UIGestureRecognizers, I'd recommend this tutorial: http://www.raywenderlich.com/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
Hope that helps!
Default UICollectionViewCell has only a blank view (no title, no delete button, no imageView)
subclass UICollectionViewCell and add delete button on it. setHidden = NO when you want to display it (Ex. swipe down)
Use custom delegate to remove data and reload collectionView
Sample use swipe right to delete UICollectionViewCell scroll vertically in storyBoard: