21

I am using a CollectionView in my ios app. Each collection cell contains a delete button. By clicking the button the cell should be deleted. After deletion, that space will be filled with below cell (I don't wish to reload the CollectionView and start from top again)

How do I delete a particular cell from UICollectionView with autolayout?

Cœur
  • 37,241
  • 25
  • 195
  • 267
New Jango Tester
  • 561
  • 3
  • 5
  • 10
  • In short: You should follow MVC protocol. Delete the data from Model and then reload your View. For more detailed answer you should post your code (only the parts where you suspect the problem is) and describe what you have tried allready... Otherwise all you'll get are downvotes instead of a good answer. Takes a good question to get a good answer. Reading [FAQ](http://stackoverflow.com/faq) doesn't hurt. – Rok Jarc Apr 24 '13 at 10:55

2 Answers2

37

UICollectionView will animate and automatically rearrange the cells after deletion.

Delete selected items from collection view

[self.collectionView performBatchUpdates:^{

    NSArray *selectedItemsIndexPaths = [self.collectionView indexPathsForSelectedItems];

    // Delete the items from the data source.
    [self deleteItemsFromDataSourceAtIndexPaths:selectedItemsIndexPaths];

    // Now delete the items from the collection view.
    [self.collectionView deleteItemsAtIndexPaths:selectedItemsIndexPaths]; 

} completion:nil];



// This method is for deleting the selected images from the data source array
-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray  *)itemPaths
{
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    for (NSIndexPath *itemPath  in itemPaths) {
        [indexSet addIndex:itemPath.row];
    }
    [self.images removeObjectsAtIndexes:indexSet]; // self.images is my data source

}
voiger
  • 781
  • 9
  • 19
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • deletion time of the cell is fixed how to make animation slow or fast. – Sishu May 24 '13 at 19:38
  • You need to subclass the UICollectionViewFlowLayout. Particularly implement finalLayoutAttributForItemAtIndexPath. Check [this](http://stackoverflow.com/questions/16690831/uicollectionview-animations-insert-delete-items) – Anil Varghese May 25 '13 at 03:15
7

No delegate methods provided to UICollectionViewController as like UITableviewController. We can do it manually by adding a long gesture recognizer to UICollectionView.

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                                         action:@selector(activateDeletionMode:)];
 longPress.delegate = self;
 [collectionView addGestureRecognizer:longPress];

In longGesture method add button on that particular cell.

- (void)activateDeletionMode:(UILongPressGestureRecognizer *)gr
{
    if (gr.state == UIGestureRecognizerStateBegan) {
        if (!isDeleteActive) {
        NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:[gr locationInView:collectionView]];
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        deletedIndexpath = indexPath.row;
        [cell addSubview:deleteButton];
        [deleteButton bringSubviewToFront:collectionView];
        }
     }
 }

In that button action,

- (void)delete:(UIButton *)sender
{
    [self.arrPhotos removeObjectAtIndex:deletedIndexpath];
    [deleteButton removeFromSuperview];
    [collectionView reloadData];
}

I think it can help you.

Islam
  • 3,654
  • 3
  • 30
  • 40
Himanshu
  • 421
  • 3
  • 10