How to delete an item from UICollectionView with indexpath.row

user2000452 picture user2000452 · Apr 30, 2013 · Viewed 41.7k times · Source

I have a collection view,and I tried to delete a cell from collection view on didSelect method.I succeeded in that using the following method

  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

But now I need to delete the item on button click from CollectionView Cell.Here I get only the indexpath.row. From this I am not able to delete the Item. I tried like this.

-(void)remove:(int)i {

    NSLog(@"index path%d",i);
   [array removeObjectAtIndex:i];

   NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
   [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
    [colleVIew reloadData];
  }

But it needs to reload the CollectionView.So the animation of cell arrangement after deletion is not there. Please suggest an idea..thanks in advance

Answer

LittleIDev picture LittleIDev · Apr 30, 2013
-(void)remove:(int)i {

    [self.collectionObj performBatchUpdates:^{
        [array removeObjectAtIndex:i];
        NSIndexPath *indexPath =[NSIndexPath indexPathForRow:i inSection:0];
        [self.collectionObj deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];

    } completion:^(BOOL finished) {

    }];
}

Try this. It may work for you.