如何删除所有项目,并添加新的项目,UICollectionView?(How to remove a

2019-08-04 04:49发布

在某些事件,

我想删除在uicollectionview所有单元格,并添加新的细胞。(将填充作为网络调用的结果)

我试着用deleteItemsAtIndexPaths。 reloadSections,deleteSections / insertSections。

他们每个人都崩溃我的应用程序。

下面是我的代码。

NSMutableArray* indexPathArray = [[NSMutableArray alloc] init];

for(int i=0; i < [self.jsonAlbumImageArray count]; ++i)
{
    NSIndexPath* newPath = [NSIndexPath indexPathForRow:i inSection:0];
    [indexPathArray addObject: newPath];
}

[self.jsonAlbumImageArray removeAllObjects];

if(indexPathArray.count > 0 )
{
    [self.collectionView performBatchUpdates:^{
            [self.collectionView deleteItemsAtIndexPaths:indexPathArray];
        }
    completion: nil];
}

// NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)];                                                                                                                                                                                        
// NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:0];                                                                                                                                                                                                                 
// [self.collectionView reloadSections:indexSet];                                                                                                                                                                                                                           

[self get_IMAGE_LIST_FROM_SERVER];

Answer 1:

是的,正如乔纳森说,我想你想要做的是用新的数据改变你的数据源和简单的“刷新”或重新加载UICollectionView。



Answer 2:

万一你需要比所提供的一个平滑的动画[的CollectionView reloadData]您可以用插入做,删除方法,这里是一个样本。

-(void)deleteItemsFromDataSourceAtIndexPaths:(NSArray  *)itemPaths
{
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    for (NSIndexPath *itemPath  in itemPaths) {
        [indexSet addIndex:itemPath.row];
    }
    [self.apps removeObjectsAtIndexes:indexSet];
}

-(void)insertItems:(NSArray*)items ToDataSourceAtIndexPaths:(NSArray  *)itemPaths
{
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    for (NSIndexPath *itemPath  in itemPaths) {
        [indexSet addIndex:itemPath.row];
    }
    [self.apps insertObjects:items atIndexes:indexSet];
}

-(void)reloadDataSmooth{
        [self.collectionView performBatchUpdates:^{

            NSMutableArray *arrayWithIndexPathsDelete = [NSMutableArray array];
            NSMutableArray *arrayWithIndexPathsInsert = [NSMutableArray array];

            int itemCount = [self.items count];

            for (int d = 0; d<itemCount; d++) {
                [arrayWithIndexPathsDelete addObject:[NSIndexPath indexPathForRow:d inSection:0]];
            }
            [self deleteItemsFromDataSourceAtIndexPaths:arrayWithIndexPathsDelete];
            [self.collectionView deleteItemsAtIndexPaths:arrayWithIndexPathsDelete];

            int newItemCount = [newItems count];

            for (int i=0; i<newAppCount; i++) {
                [arrayWithIndexPathsInsert addObject:[NSIndexPath indexPathForRow:i inSection:0]];
            }
            [self insertItems:newItems ToDataSourceAtIndexPaths:arrayWithIndexPathsInsert];

            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPathsInsert];
        }
        completion:nil];
    }
}


文章来源: How to remove all items and add new items to UICollectionView?