如何使用indexpath.row从UICollectionView中删除项目

17 iphone ios uicollectionview uicollectionviewcell

我有一个集合视图,我试图从didSelect方法的集合视图中删除一个单元格.我成功地使用以下方法

  [colleVIew deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
Run Code Online (Sandbox Code Playgroud)

但现在我需要从CollectionView Cell中删除按钮上的项目.这里我只得到indexpath.row.由此我无法删除该项目.我试过这样的.

-(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];
  }
Run Code Online (Sandbox Code Playgroud)

但它需要重新加载CollectionView.So删除后的单元格排列动画不存在.请提出一个想法..提前谢谢

Lit*_*Dev 57

-(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) {

    }];
}
Run Code Online (Sandbox Code Playgroud)

试试这个.它可能适合你.


bux*_*xik 13

Swift 3解决方案:

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

}
Run Code Online (Sandbox Code Playgroud)

示例删除调用:

self.remove(indexPath.row)
Run Code Online (Sandbox Code Playgroud)


Moh*_*ani 5

快速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)
    })
}
Run Code Online (Sandbox Code Playgroud)