ios 从 UICollectionView 删除单元格而不从顶部重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16189682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:19:25  来源:igfitidea点击:

Delete cell from UICollectionView without reloading from top

iosuicollectionviewuicollectionviewcelluicollectionviewlayout

提问by New Jango Tester

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)

我在我的 ios 应用程序中使用 CollectionView。每个集合单元格都包含一个删除按钮。通过单击按钮,应删除单元格。删除后,该空间将被下面的单元格填充(我不想重新加载 CollectionView 并再次从顶部开始)

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

如何使用自动布局从 UICollectionView 中删除特定单元格?

回答by Anil Varghese

UICollectionViewwill animate and automatically rearrange the cells after deletion.

UICollectionView将动画并在删除后自动重新排列单元格。

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

}

回答by Himanshu

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

没有像 UITableviewController 那样提供给 UICollectionViewController 的委托方法。我们可以通过向 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.

在 longGesture 方法中,在该特定单元格上添加按钮。

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

我认为它可以帮助你。