Add more UICollectionViewCell to an existing UICollectionView

14,681

Solution 1

The easiest way to insert new cells to the UICollectionView without having to reload all its cell is by using the performBatchUpdates, which can be done easily by following the steps below.

// Lets assume you have some data coming from a NSURLConnection
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *erro)
{
      // Parse the data to Json
      NSMutableArray *newJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

      // Variable used to say at which position you want to add the cells
      int index;

      // If you want to start adding before the previous content, like new Tweets on twitter
      index = 0;

      // If you want to start adding after the previous content, like reading older tweets on twitter
      index = self.json.count;

      // Create the indexes with a loop
      NSMutableArray *indexes = [NSMutableArray array];

      for (int i = index; i < json.count; i++)
      {
            [indexes addObject:[NSIndexPath indexPathForItem:i inSection:0]];
      }

      // Perform the updates
      [self.collectionView performBatchUpdates:^{

           //Insert the new data to your current data
           [self.json addObjectsFromArray:newJson];

           //Inser the new cells
           [self.collectionView insertItemsAtIndexPaths:indexes];

      } completion:nil];
 }

Solution 2

The UICollectionView class has methods to add/remove items. E.g., to insert an item at some index (in section 0), modify your model accordingly and then do:

int indexPath = [NSIndexPath indexPathForItem:index];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath inSection:0];
[collectionView insertItemsAtIndexPaths:indexPaths];

The view will do the rest.

Share:
14,681
Anderson Bressane
Author by

Anderson Bressane

Updated on July 18, 2022

Comments

  • Anderson Bressane
    Anderson Bressane almost 2 years

    I'm trying to add some more cells to an existing UICollectionView, which is already filled with some cells.

    I tried to use the CollectionView reloadData but it seems to reload the entire collectionView and I just wanted to add more cells.

    Can anybody help me?

  • Anderson Bressane
    Anderson Bressane over 11 years
    So, when i first use [UICollectionView reloadData] i should save the index of the last Cell used, and then, when i call the method to add more cells i should use its latest cell indexPath, correct? @chris
  • chris
    chris over 11 years
    You don't need [UICollectionView reloadData] at all when adding items. Just do insertItemsAtIndexPaths: and make sure that subsequent calls to your data source' methods collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: reflect your changes.
  • Anderson Bressane
    Anderson Bressane over 11 years
    I did it at first it worked. But, when the new cells will be show on the display i get the message: Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (36) beyond bounds (36)' @chris do you know what i did wrong?
  • Anderson Bressane
    Anderson Bressane over 11 years
    This is the line i coded: NSMutableArray *indexes = [NSMutableArray array]; for (int i = 0; i < [_jsonLista count]; i++) { NSIndexPath *index = [NSIndexPath indexPathForItem:([Collection numberOfItemsInSection:0]) + i inSection:0]; [indexes addObject:index]; } [Collection insertItemsAtIndexPaths:indexes];
  • chris
    chris over 11 years
    You have to adjust your model before updating the view.
  • Anderson Bressane
    Anderson Bressane over 11 years
    I changed the numberOfItemsInSection to the new value, which is 72 now. Is that it? @Chris - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [_totalGrid intValue]; }
  • Mansuu....
    Mansuu.... over 6 years
    this method disappears previous cells then load again and scrolls collectionView to the top.