UICollectionView doesn't scroll full 320 points with paging enabled

476 views Asked by At

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.
}
1

There are 1 answers

9
brandonscript On

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).