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

Ios 从UICollectionView中删除单元格而不从顶部重新加载,ios,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我正在ios应用程序中使用CollectionView。每个收集单元都包含一个删除按钮。通过单击按钮,应删除单元格。删除后,该空间将填充以下单元格(我不希望重新加载CollectionView并再次从顶部开始) 如何使用autolayout从UICollectionView中删除特定单元格?没有像UITableviewController那样向UICollectionViewController提供委托方法。 我们可以通过向UICollectionView添加一个长手势识别器来手动完成 UIL

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


如何使用autolayout从UICollectionView中删除特定单元格?

没有像UITableviewController那样向UICollectionViewController提供委托方法。 我们可以通过向UICollectionView添加一个长手势识别器来手动完成

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                                         action:@selector(activateDeletionMode:)];
 longPress.delegate = self;
 [collectionView addGestureRecognizer:longPress];
在长手势方法中,在特定单元格上添加按钮

- (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];
        }
     }
 }
在那个按钮动作中

- (void)delete:(UIButton *)sender
{
    [self.arrPhotos removeObjectAtIndex:deletedIndexpath];
    [deleteButton removeFromSuperview];
    [collectionView reloadData];
}

我认为它可以帮助您。

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

从收藏视图中删除所选项目

[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

}

简而言之:您应该遵循MVC协议。从模型中删除数据,然后重新加载视图。要获得更详细的答案,你应该发布你的代码(只有你怀疑问题所在的部分)并描述你已经尝试过的allready。。。否则,你只能得到反对票,而不是一个好答案。需要一个好问题才能得到一个好答案。读取不会造成伤害。单元格的删除时间是固定的,如何使动画变慢或变快。您需要对UICollectionViewFlowLayout进行子类化。特别是实现FinallYoutAttributeForItemAtIndexPath。选中
[deleteButton将子视图带到前面:collectionView]似乎是一个错误。