gpt4 book ai didi

iphone - 如何删除所有项目并向 UICollectionView 添加新项目?

转载 作者:可可西里 更新时间:2023-11-01 03:40:05 25 4
gpt4 key购买 nike

在某些事件上,

我想删除 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];

最佳答案

如果您需要比 [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];
}
}

关于iphone - 如何删除所有项目并向 UICollectionView 添加新项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13914080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com