私も同様の問題に遭遇し、かなりハックな感じで解決しましたが、うまくいき、今のところ満足しています。
基本的に私がやろうと決めたのは、コレクションビューページを空のUICollectionViewCellsで埋めることでした。
まず、アイテムの総数を計算します。NSFetchedResultsControllerによって提供されるアイテムの総数が正確に3の倍数でない場合は、必要な数のアイテムを追加して、合計が3の倍数になるようにします。
たとえば、4つのカテゴリを取り戻した場合、さらに2つ追加して、合計6つにする必要があります。
#define ITEMS_PER_PAGE 3
//...
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSArray *sections = [categoriesFetchedResultsController sections];
id <NSFetchedResultsSectionInfo> sectionInfo = sections[section];
NSInteger categoryCount = [sectionInfo numberOfObjects];
NSInteger extraItemsNeeded = ITEMS_PER_PAGE - (categoryCount % ITEMS_PER_PAGE);
NSInteger totalItems = categoryCount + extraItemsNeeded;
NSInteger pages = (NSInteger)(totalItems / ITEMS_PER_PAGE);
self.pageControl.numberOfPages = pages;
return totalItems;
}
次に、セルを生成するときが来たら、次のようにします。
- (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;
}
この問題の解決は本来あるべきほど簡単ではなかったので、これが将来誰かに役立つことを願っています。また、フローレイアウトがページを適切に自動的に計算しなかったことに驚きました。