简体   繁体   English

启用分页时如何扩展 UICollectionView contentSize?

[英]How to expand UICollectionView contentSize when paging enable?

I'm making a simple UICollectionView with a paging mechanism enabled, and everything works fine.我正在制作一个启用了分页机制的简单UICollectionView ,并且一切正常。 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?如何扩展UICollectionViewcontentSize以便最后一页不包含上一页的任何单元格?

An example here : the UICollectionView scrolls horizontally with 6 cells, this way: 这里的一个例子: UICollectionView水平滚动 6 个单元格,这样:

  • Page 1:第 1 页:

    cell0 - cell1 - cell2 - cell3

  • Page 2:第2页:

    cell4 - cell5 is expected, but unexpectedly cell4 - cell5是预期的,但出乎意料

    cell2 - cell3 - cell4 - cell5

How to change it?如何改变它?

SUMMARY:概括:

I want to set我想设置

collectionView.contentSize = numberOfPage * collectionView.frame

NOT不是

collectionView.contentSize = numberOfCell * (cellFrame + spacing)

You need to subclass UICollectionViewLayout and override the collectionViewContentSize method.您需要UICollectionViewLayout并覆盖collectionViewContentSize方法。 I subclassed UICollectionViewFlowLayout so I wouldn't have to re-write all the layout code.我将UICollectionViewFlowLayout子类UICollectionViewFlowLayout因此我不必重新编写所有布局代码。

I'm building a 4x4 grid, so my method looks like this:我正在构建一个 4x4 网格,所以我的方法如下所示:

- (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.旁注,当您使用自定义布局时,您将无法在 Interface Builder 中设置某些显示属性。 You can set them programatically in the init method of your custom UICollectionViewLayout subclass.您可以在自定义 UICollectionViewLayout 子类的init方法中以编程方式设置它们。 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];
}

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);
}

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)) ;
}

You can adjust the content with the viewDidLayoutSubviews: method.您可以使用viewDidLayoutSubviews:方法调整内容。 This method gets called when the collection view and all the cells are placed in the view , so that you can adjust cell.当集合视图和所有单元格放置在view ,将调用此方法,以便您可以调整单元格。

我认为您必须UICollectionViewLayout并创建自定义布局来管理此类问题。

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.在 -viewDidLoad 中,计算您将拥有的每页项目数以及您将拥有的页面数。 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:然后在 -collectionView:numberOfItemsInSection: 中撒谎:

    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:所以在 -collectionView:cellForItemAtIndexPath: 中只为额外的单元格返回 nil:

    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.在我看来,水平滚动模式应该只是默认为这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM