user2000452
user2000452

Reputation:

How to delete an item from UICollectionView with indexpath.row

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

Upvotes: 23

Views: 45463

Answers (7)

mistdon
mistdon

Reputation: 1883

The important thing is which IndexPath you choose.

[self.collectionView performBatchUpdates:^{
      NSIndexPath *currentIndexPath = [self.collectionView indexPathForCell:cell];
     [self.datas removeObjectAtIndex:currentIndexPath.row];
     [self.collectionView deleteItemsAtIndexPaths:@[currentIndexPath]];
} completion:nil];

Upvotes: 0

user1025285
user1025285

Reputation:

[array removeObjectAtIndex:[indexPath row]];
    [collection reloadData]; // Collection is UICollectionView

Try this.

Upvotes: 4

buxik
buxik

Reputation: 2625

Swift 3 solution:

func remove(_ i: Int) {

    myObjectsList.remove(at: i)

    let indexPath = IndexPath(row: i, section: 0)

    self.collectionView.performBatchUpdates({
        self.collectionView.deleteItems(at: [indexPath])
    }) { (finished) in
        self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
    }

}

and example delete call:

self.remove(indexPath.row)

Upvotes: 21

Mohammad Alikhani
Mohammad Alikhani

Reputation: 191

swift 3:

func remove(index: Int) {
    myObjectList.remove(at: index)

    let indexPath = IndexPath(row: index, section: 0)
    collectionView.performBatchUpdates({
        self.collectionView.deleteItems(at: [indexPath])
    }, completion: {
        (finished: Bool) in
        self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
    })
}

Upvotes: 5

LittleIDev
LittleIDev

Reputation: 921

-(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.

Upvotes: 62

user2000452
user2000452

Reputation:

I got the answer..

Create a button in CollectionViewCell // I named it as removeBtn

Then in CollectionView Delegate

 - cellForItemAtIndexPath

   [cell.removeBtn addTarget:self action:@selector(RemovePrssd:) forControlEvents:UIControlEventTouchUpInside];

Then add the method

-(void)RemovePrssd:(id)sender{

 UIView *senderButton = (UIView*) sender;
 NSIndexPath *indexPath = [colleVIew indexPathForCell: (UICollectionViewCell *)[[senderButton superview]superview]];

  [array removeObjectAtIndex:indexPath.row];
  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
  }

Upvotes: 0

Plokstorm
Plokstorm

Reputation: 27

[array removeObjectAtIndex:[indexPath row]];

[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];

Normally it's ok... You can see this post, it deals with same subject.

Upvotes: 1

Related Questions