How to delete an item from UICollectionView with indexpath.row

ε祈祈猫儿з 提交于 2019-12-02 20:41:07
-(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.

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)

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)
    })
}
[array removeObjectAtIndex:[indexPath row]];
    [collection reloadData]; // Collection is UICollectionView

Try this.

Plokstorm

[array removeObjectAtIndex:[indexPath row]];

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

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

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]];
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!