0

enter image description here顯示第一頁

上出於某種原因,當我UICollectionView的第一頁上UICollectionView第二頁內容, 它顯示從第二頁的圖像。

而且我還注意到,該網頁不居中....

我手動設置

pageControl.numberOfPages = 3 

所以,我有三個頁面圖像。 每個頁面顯示8項(理想情況下......)。

這是在viewDidLoad中:

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init]; 
[layout setSectionInset:UIEdgeInsetsMake(10, 10, 15, 10)]; 
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 
[self.collectionView setCollectionViewLayout:layout]; 
self.collectionView = [[UICollectionView alloc] initWithFrame:keyboardRect collectionViewLayout:layout]; 
self.collectionView.dataSource = self; 
self.collectionView.delegate = self; 
self.collectionView.pagingEnabled = true; 
self.collectionView.alwaysBounceHorizontal = true; 
self.collectionView.contentSize = CGSizeMake(keyboardRect.size.width , keyboardRect.size.height); 
self.collectionView.showsHorizontalScrollIndicator = false; 
UINib *cellNib = [UINib nibWithNibName:@"TopCell" bundle:nil]; 
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cellIdentifier"]; 
[self.collectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; 
[self.collectionView setBackgroundColor:[UIColor redColor]]; 
[self.view addSubview:self.collectionView]; 

當我試圖從

[layout setSectionInset:UIEdgeInsetsMake(10, 10, 15, 10)]; 

它只是使事情更糟提高左,右插圖。它只會在第一頁上看起來很好, 但是第二頁和第三頁將會關閉。

這裏是我的一些代碼:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return [self.Images count]; 
} 


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 
    return CGSizeMake(70, 70); 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView { 
    return 3; 
} 

預先感謝您!

+0

爲collectionview和check設置pagingEnabled = TRUE。 – iOSEnthusiatic

+0

它設置爲true。 (上面的代碼) –

回答

0

如果你想只顯示一個頁面的每個截面圖像,那麼你必須修改

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath方法。

您必須返回更一般的尺寸。

CGSize returnSize = CGSizeMake(screenWidth/2 - insets, screenWidth/2 - insets)

這裏插圖表示每個項目的interItemSpacing。 screenWidth是collectionView的寬度。