ios 启用分页时如何扩展 UICollectionView contentSize?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14852004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to expand UICollectionView contentSize when paging enable?
提问by LE SANG
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.
我在UICollectionView
启用分页机制的情况下制作了一个简单的东西,并且一切正常。但是,当滚动到最后一页时,屏幕中的单元格数量并不完全可见,最后一页包含上一页的一些单元格。
How do I expand the contentSize
of the UICollectionView
so that the last page doesn't contain any cells of the previous page?
如何扩大contentSize
的UICollectionView
,这样最后一页不包含前一页的任何细胞?
An example here: the UICollectionView
scrolls horizontally with 6 cells, this way:
这里的一个例子:UICollectionView
水平滚动 6 个单元格,这样:
Page 1: cell0 - cell1 - cell2 - cell3
第 1 页: cell0 - cell1 - cell2 - cell3
Scroll: cell2-cell3-cell4-cell5 // Not expected: how to change to: cell4 - cell 5 in page2.
滚动:cell2-cell3-cell4-cell5 // 非预期:如何更改为:cell4 - page2 中的cell 5。
SUMMARY:
概括:
I want to set
我想设置
collectionView.contentSize = numberOfPage * collectionView.frame
collectionView.contentSize = numberOfPage * collectionView.frame
NOT
不是
collectionView.contentSize = numberOfCell * (cellFrame + spacing)
collectionView.contentSize = numberOfCell * (cellFrame + spacing)
回答by reallyseth
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.
您需要子类化UICollectionViewLayout
并覆盖该collectionViewContentSize
方法。我进行了子类化,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. You can set them programatically in the init
method of your custom UICollectionViewLayout subclass. Here's mine for reference:
旁注,当您使用自定义布局时,您将无法在 Interface Builder 中设置某些显示属性。您可以在init
自定义 UICollectionViewLayout 子类的方法中以编程方式设置它们。这是我的供参考:
- (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];
}
回答by user2501116
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);
}
回答by christophercotton
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)) ;
}
回答by Paul Bruneau
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.
在 -viewDidLoad 中,计算您将拥有的每页项目数以及您将拥有的页面数。将这些值存储在属性中。
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.
你去吧:完整的、可滚动的最后一页。在我看来,水平滚动模式应该只是默认为这个。
回答by Swapnil
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.
您可以使用viewDidLayoutSubviews:
方法调整内容。当集合视图和所有单元格放置在 中时view
,将调用此方法,以便您可以调整单元格。
回答by Andrea Mario Lufino
I think you have to subclass UICollectionViewLayout
and create your custom layout to manage these kind of problems.
我认为您必须子类化UICollectionViewLayout
并创建自定义布局来管理此类问题。