1

好吧,所以可以说我有UICollection 20个细胞与启用分页水平滚动和可容纳每一页上9个细胞,当我提出它,而不是10个细胞创建第二页面移动到足以适应页面上的第10个单元格。当我滚动时,我希望它在自己的页面上显示第10个单元格。UICollectionView不滚动完整320个分页启用

此外,我试图改变collectionview.contentsize但由于某种原因它保持相同的,无论我做什么。

这里是我的代码 -

- (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. 
} 
+0

这就是默认流程布局的工作方式。您可以增加单元格的大小,增加单元格之间的最小间距,和/或增加sectionInset。 – rdelmar 2014-12-13 01:01:15

回答

1

你不能强迫集合视图完全滚动到这样的不幸了新的一页。

相反,才算填写页面,空的添加到您的数据集的末尾所需的细胞数量。

例如,如果你在你的数据的NSArray了19个项目,再添8,使总量达到27

在你collectionView:numberOfItemsInSection:的委托,做这样的事情:

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

只需确保在cellForItemAtIndexPath中执行应急操作以显示空白单元格(因为您的数据集实际上不会包含该项目的数据)。

+0

我想它的问题是,当我滚动的空白单元格被重用,因此空格显示所有的地方 – user3296487 2014-12-13 02:05:19

+0

确保在处置willDisplayItemAtIndexPath你的细胞正常了。 – brandonscript 2014-12-13 02:06:04

+0

即使在创建它们之前也会删除它们,并使应用程序崩溃 – user3296487 2014-12-13 02:19:47