1

Alright so lets say I have UICollection with 20 cells That scrolls horizontally with paging enabled and can fit 9 cells on each page, when I make it 10 cells instead of creating a second page it moves just enough to fit the 10th cell on the page. I want it to display the 10th cell on its own page when I scroll over.

Also i have tried changing the collectionview.contentsize but for some reason it stays the same no matter what i do.

Here is my code -

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(82, 119);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(20, 10, 50, 10);
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    customCell *cell= (customCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];


    return (UICollectionViewCell*)cell;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

- (void)viewDidLoad {





    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    _collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [_collectionView setDataSource:self];
    [_collectionView setDelegate:self];

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
    [_collectionView setBackgroundColor:[UIColor lightGrayColor]];


    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    _collectionView.pagingEnabled = YES;

    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 25;

    [_collectionView setContentInset:UIEdgeInsetsMake(65, 0, 0, 0)];



    _collectionView.allowsMultipleSelection = YES;
    [self.view addSubview:_collectionView];



    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
user3296487
  • 326
  • 2
  • 12
  • That's the way the default flow layout works. You can increase the size of the cells, increase the minimum spacing between the cells, and/or increase the sectionInset. – rdelmar Dec 13 '14 at 01:01

1 Answers1

2

You can't force the collection view to completely scroll to a new page like that unfortunately.

Instead, just calculate the number of cells required to fill out the page and add empty ones to the end of your dataset.

For example, if you've got 19 items in your NSArray of data, add another 8 to bring the total up to 27.

In your collectionView:numberOfItemsInSection: delegate, do something like:

return (dataArray.count % 9 == 0) ? dataArray.count : ((dataArray.count/9|0)+1)*9;
// 17 returns 18
// 19 returns 27

Just make sure you perform a contingency operation in cellForItemAtIndexPath to display a blank cell (since your dataset won't actually contain data for that item).

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • I tried it the problem is when I scroll those blank cells are reused therefore blank spaces show up all over the place – user3296487 Dec 13 '14 at 02:05
  • Make sure you dispose of your cells properly too in willDisplayItemAtIndexPath. – brandonscript Dec 13 '14 at 02:06
  • It deletes the cells even before they are created and crashes my app – user3296487 Dec 13 '14 at 02:19
  • That'll happen if you don't generate the correct number of cells for your data. So you need to check whether the row of data exists before generating a blank one. – brandonscript Dec 13 '14 at 02:32
  • Have you tested this before? cause I can't quite get it to work – user3296487 Dec 13 '14 at 03:15
  • Yep, I've done something similar before. Takes a bit of fiddling, but it works. – brandonscript Dec 13 '14 at 03:21
  • brilliant one.. I returned such fake values and handled the data to be populated from the array in my cellForItemAtIndexPath.. +1 – Ahsan Ebrahim Khatri Oct 29 '15 at 10:45
  • I'm trying to figure out the same issue. Can you post some code on how to create the blank cells in the remainder to fully fill out the collectionview? Can't get it to work. Thanks!! – tahoecoop Dec 07 '15 at 04:37
  • @remus Thanks for the response. I get the code to attach additional cells to the end of the dataset. I'm just uncertain how to add the blank cells themselves in cellForItemAtIndexPath. I appreciate it! – tahoecoop Dec 07 '15 at 04:58