从 UICollectionView 中删除单元格而不从顶部重新加载 [英] Delete cell from UICollectionView without reloading from top

查看:17
本文介绍了从 UICollectionView 中删除单元格而不从顶部重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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

推荐答案

UICollectionView 会在删除后自动重新排列单元格.

UICollectionView will animate and automatically rearrange the cells after deletion.

从集合视图中删除所选项目

[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

}

这篇关于从 UICollectionView 中删除单元格而不从顶部重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆