How to expand UICollectionView contentSize when pa

2019-01-16 12:43发布

I'm making a simple UICollectionView with a paging mechanism enabled, and everything works fine. Though, when scroll to the last page the number of the cells are not fully visible in the screen, and the last page contains some cells of the previous page.

How do I expand the contentSize of the UICollectionView so that the last page doesn't contain any cells of the previous page?

An example here: the UICollectionView scrolls horizontally with 6 cells, this way:

Page 1: cell0 - cell1 - cell2 - cell3

Scroll: cell2-cell3-cell4-cell5 // Not expected: how to change to: cell4 - cell 5 in page2.

SUMMARY:

I want to set

collectionView.contentSize = numberOfPage * collectionView.frame

NOT

collectionView.contentSize = numberOfCell * (cellFrame + spacing)

6条回答
地球回转人心会变
2楼-- · 2019-01-16 13:12

I have an answer that doesn't require any subclassing.

In -viewDidLoad, calculate how many items per page you will have and how many pages you will have. Store these values in properties.

    self.numberOfItemsPerPage = NumberOfRows * NumberOfColumns;
    self.numberOfPages = ceilf((CGFloat)self.items.count / (CGFloat)self.numberOfItemsPerPage);

Then in -collectionView:numberOfItemsInSection: just lie to it:

    return self.numberOfItemsPerPage * self.numberOfPages;

You will of course have more cells than content, right? So in -collectionView:cellForItemAtIndexPath: just return nil for the extra cells:

    if (indexPath.item > [self.items count] - 1) {
    //We have no more items, so return nil. This is to trick it to display actual full pages.
    return nil;
    }

There you go: full, scrollable final page. In my opinion, the horizontal scroll mode should just default to this.

查看更多
小情绪 Triste *
3楼-- · 2019-01-16 13:18

For horizontal paging

if(CollectionView.pagingEnabled)
{

    int numberOfPages = floor(collectionView.contentSize.width /
                      collectionView.frame.size.width) + 1;
                             CGFloat 
    float requierdWidth=self.collectionView.frame.size.width*numberOfPages;

    self.Layout.footerReferenceSize=CGSizeMake(requierdWidth-self.collectionView.contentSize.width,0);
}
查看更多
女痞
4楼-- · 2019-01-16 13:19

I think you have to subclass UICollectionViewLayout and create your custom layout to manage these kind of problems.

查看更多
Deceive 欺骗
5楼-- · 2019-01-16 13:26

You need to subclass UICollectionViewLayout and override the collectionViewContentSize method. I subclassed UICollectionViewFlowLayout so I wouldn't have to re-write all the layout code.

I'm building a 4x4 grid, so my method looks like this:

- (CGSize)collectionViewContentSize
{    
    NSInteger itemCount = [self.collectionView numberOfItemsInSection:0];
    NSInteger pages = ceil(itemCount / 16.0);

    return CGSizeMake(320 * pages, self.collectionView.frame.size.height);
}

Side note, when you use a custom layout, you lose the ability to set the some of the display properties in the Interface Builder. You can set them programatically in the init method of your custom UICollectionViewLayout subclass. Here's mine for reference:

- (id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        [self setup];
    }

    return self;
}

- (void)setup
{
    self.itemSize = CGSizeMake(65.0f, 65.0f);
    self.minimumLineSpacing = 15;
    self.sectionInset = UIEdgeInsetsMake(7.5f, 7.5f, 30.0f, 7.5f);
    [self setScrollDirection:UICollectionViewScrollDirectionHorizontal];
}
查看更多
狗以群分
6楼-- · 2019-01-16 13:34

The answer works well, though for our code we had one section per page. So it meant the override for our layout class was just

-(CGSize) collectionViewContentSize {
    return CGSizeMake(CGRectGetWidth(self.collectionView.frame) * 
                 [self.collectionView numberOfSections],
                 CGRectGetHeight(self.collectionView.frame)) ;
}
查看更多
混吃等死
7楼-- · 2019-01-16 13:38

You can adjust the content with the viewDidLayoutSubviews: method. This method gets called when the collection view and all the cells are placed in the view, so that you can adjust cell.

查看更多
登录 后发表回答