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.
Author by
Anderson Bressane
Updated on July 18, 2022Comments
-
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 over 11 yearsSo, 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 over 11 yearsYou don't need
[UICollectionView reloadData]
at all when adding items. Just doinsertItemsAtIndexPaths:
and make sure that subsequent calls to your data source' methodscollectionView:numberOfItemsInSection:
andcollectionView:cellForItemAtIndexPath:
reflect your changes. -
Anderson Bressane over 11 yearsI 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 over 11 yearsThis 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 over 11 yearsYou have to adjust your model before updating the view.
-
Anderson Bressane over 11 yearsI 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.... over 6 yearsthis method disappears previous cells then load again and scrolls collectionView to the top.