I had a similar problem, and I decided it was pretty hacky, but it works, and I'm happy with it for now.
Basically, what I decided to do was fill the collection view pages with an empty UICollectionViewCells.
I first calculated how many total items there will be. If the total number of elements provided by my NSFetchedResultsController is not an exact multiple of three, then I add the required number of elements so that the sum is a multiple of three.
For example, if I get four categories back, I need to add two more to make them a total of six:
#define ITEMS_PER_PAGE 3
Then, when it comes time to generate cells, I do the following:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryCell" forIndexPath:indexPath]; // use a generic UICollectionCell for the "empty cell" case. UICollectionViewCell *emptyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EmptyCell" forIndexPath:indexPath]; if (indexPath.row < [[categoriesFetchedResultsController fetchedObjects] count] ) { NSManagedObject *object = [categoriesFetchedResultsController objectAtIndexPath:indexPath]; NSData *imageData = [object valueForKey:@"thumbnail_data"]; NSString *name = [object valueForKey:@"category_name"]; cell.image.image = [UIImage imageWithData:imageData]; cell.label.text = name; return cell; } return emptyCell; }
I hope this helps someone in the future, as the solution to this problem was not as simple as it should have been, and I was surprised that the layout of the stream does not just automatically calculate the pages accordingly.
Evan K. Stone
source share