UICollectionView Horizontal Scrolling Shows Previous Items Even When Paging Is Enabled

I am implementing a uicollection view with the scroll direction set to horizontal. I also enabled the swap option in the storyboard. In my opinion, I can display 4 elements at a time. If I have 5 elements, there are 4 elements on the first page and 3 elements on the second page. Previous items are also displayed on the second page.

I just want to ask how can I prevent this? I want 4 elements on the first page and only 1 element on the second page. I am thinking of adding elements and hiding them for this. Is this the right way to resolve this issue?

Many thanks!

+7
source share
2 answers

Perhaps you could set the contentSize property of UICollectionView to the next multiple of its bounds ?

eg. for a single-sector presentation of the collection:

 NSUInteger itemsPerPage = 4; NSUInteger pageCount = (NSUInteger) ceilf((float)[collectionView.dataSource numberOfItemsInSection:0] / itemsPerPage); CGFloat contentWidth = collectionView.bounds.size.width * pageCount; collectionView.contentSize = CGSizeMake(contentWidth, collectionView.contentSize.height); 
0
source

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 //... - (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; } 

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.

0
source

All Articles