6

On certain events,

I'd like to remove all cells in a uicollectionview, and add new cells.(which will be populated as a result of a network call)

I tried using deleteItemsAtIndexPaths. reloadSections, deleteSections/insertSections.

Each of them crashed my app.

Below is my code.

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];
eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

7

Just in case you need a smoother animation than the one provided by [collectionView reloadData] you can do it with the insert and delete methods, here is a sample.

-(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];
    }
}
PakitoV
  • 2,476
  • 25
  • 34
5

Yeah, as Jonathan said, I think what you want to do is change your datasource with the new data and simply "refresh" or reload the UICollectionView.

Travis M.
  • 10,930
  • 1
  • 56
  • 72
  • What so you saying if you clear your data source completely and do a ```[collectionView reloadData]``` the app won't crash and the collection view will be cleared? – Supertecnoboff Oct 05 '15 at 20:25
  • if by "clear your data source" you mean make it a 0 count object, then yes, that is exactly what will happen. However, if you make it "nil" you may get unexpected results, depending on if you're using swift or obj-c. – Travis M. Oct 05 '15 at 20:54
  • 1
    Awesome thanks Travis and yes, that is what I meant :) – Supertecnoboff Oct 05 '15 at 20:55