UICollectionView how to delete cells (equivalent o

2019-03-16 02:23发布

问题:

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)?

回答1:

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.

- (IBAction)didLongPressCellToDelete:(UILongPressGestureRecognizer*)gesture {
CGPoint tapLocation = [gesture locationInView:self.myCollectionView];
NSIndexPath *indexPath = [self.myCollectionView indexPathForItemAtPoint:tapLocation];
if (indexPath && gesture.state == UIGestureRecognizerStateBegan) {
    NSLog(@"image with index %d to be deleted", indexPath.item);
    self.itemToBeDeleted = indexPath.item;
    UIAlertView *deleteAlert = [[UIAlertView alloc]
                             initWithTitle:@"Delete?"
                             message:@"Are you sure you want to delete this image?"
                             delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
    [deleteAlert show];

}
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"selected button index = %d", buttonIndex);
if (buttonIndex == 1) {
    // Do what you need to do to delete the cell 
    [self.myCollectionView reloadData];
}
}


回答2:

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:

//Cell.h
@class MyCell;
@protocol MyCellDelegate
-(void)deleteButtonClick:(MyCell *)cell;
@end
@interface MyCell : UICollectionViewCell
@property (weak , nonatomic) id<MyCellDelegate> delegate;
@property (weak,nonatomic) IBOutlet *delButton;
@end

//Cell.m
-(void)awakeFromNib{
  [self.delButton setHidden:YES]
  //add Swipe right to here
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
   [self.delButton setHidden:NO];
}
-(IBAction)clickDelBut{
 [self.delegate deleteButtonClick:self];
}

//ViewController.m
//In cellForItemsAtIndexPath cell.delegate = self.

-(void)deleteButtonClick:(MyCell *)cell{
  //get indexPath, delete data and reload collectionView here
}


回答3:

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/



回答4:

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!