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