【问题标题】:How to remove all items and add new items to UICollectionView?如何删除所有项目并将新项目添加到 UICollectionView?
【发布时间】:2012-12-17 12:21:11
【问题描述】:

在某些事件中,

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

标签: iphone ios uicollectionview


【解决方案1】:

如果您需要比 [collectionView reloadData] 提供的更流畅的动画,您可以使用 insert 和 delete 方法来实现,这里有一个示例。

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

【讨论】:

  • 这里的self.apps 是什么?
  • @TejaswiYerukalapudi CollectionCells 数组(可变)
【解决方案2】:

是的,正如 Jonathan 所说,我认为您想要做的是使用新数据更改您的数据源,然后简单地“刷新”或重新加载 UICollectionView。

【讨论】:

  • 如果您完全清除数据源并执行[collectionView reloadData],应用程序不会崩溃并且集合视图将被清除,那您说什么?
  • 如果“清除数据源”是指将其设为 0 计数对象,那么是的,这正是将会发生的事情。但是,如果将其设为“nil”,则可能会得到意想不到的结果,具体取决于您使用的是 swift 还是 obj-c。
  • 非常感谢特拉维斯,是的,这就是我的意思:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多