-->

删除从UICollectionView细胞不脱离顶部重装[关闭](Delete cell from

2019-09-01 00:43发布

我用我的iOS应用的CollectionView。 每个集合单元包含一个删除按钮。 通过点击按钮,电池应予以删除。 删除后,该空间将充满以下细胞(我不想重装的CollectionView和从顶部重新开始)

如何从UICollectionView删除特定的细胞与自动布局?

Answer 1:

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

}


Answer 2:

提供给UICollectionViewController像的UITableViewController没有委托方法。 我们可以通过添加一个长的手势识别来UICollectionView做手工。

 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                                         action:@selector(activateDeletionMode:)];
 longPress.delegate = self;
 [collectionView addGestureRecognizer:longPress];

在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];
        }
     }
 }

在该按钮的动作,

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

我认为它可以帮助你。



文章来源: Delete cell from UICollectionView without reloading from top [closed]