0

我正在嘗試將我的UICollectionView排除,使其成爲每頁2x3網格。以下是我迄今爲止得到:鋪設UICollectionView以創建2x3網格的問題iOS

enter image description here

正如你所看到的,接下來的「頁」是從權,我不想偷窺。我想如果我知道我的寬度始終是320pts(僅限於iPhone),那麼我希望我的細胞在width之間變爲130pts,兩邊都是15pts。

代碼佈局delegate

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout*)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    return CGSizeMake(130, 130); 
} 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView 
         layout:(UICollectionViewLayout*)collectionViewLayout 
     insetForSectionAtIndex:(NSInteger)section { 
    return UIEdgeInsetsMake(20.0f, 15.0f, 20.0f, 15.0f); 
} 

而且還cellForItemAtIndexPath:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
       cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    CustCollectionViewCell *cell = 
     [collectionView dequeueReusableCellWithReuseIdentifier:kReuseID 
              forIndexPath:indexPath]; 

    //Set up list name label 
    cell.LblListName.font = [self getDefaultFontWithSize:15.0f]; 
    cell.LblListName.textColor = fontAndTertiaryColor; 
    cell.LblListName.textAlignment = NSTextAlignmentCenter; 
    cell.LblListName.text = @"Failtron"; 
    //cell.LblListName.adjustsFontSizeToFitWidth = YES; 

    cell.backgroundColor = secondaryColor; 
    cell.parallaxIntensity = kParallaxMotion; 
    [Utilities createViewBorder:cell]; 

    return cell; 
} 

我想到了一個插圖將在CSSpadding,但我相信我跟那個一個標誌,我還應該注意到,在UIStoryboard我的佈局設置爲流動。請讓我知道,如果我錯過了任何相關的代碼,這是我第一次嘗試UICollectionView。我想這也是scrollhorizontally

+0

所以你希望collectionview水平滾動,但你不想看到下一頁。是對的嗎? – jhilgert00

+0

是的,先生,這是正確的 –

回答

1

好,有噸的方法來操作的UICollectionView佈局,但聽起來,因爲使用的是「流動」你可以實現<UICollectionViewDelegateFlowLayout>.h,然後用下面的方法來佈局調整自己的喜好:

#pragma mark – UICollectionViewDelegateFlowLayout 

- (CGSize)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout*)collectionViewLayout 
    sizeForItemAtIndexPath:(NSIndexPath *)indexPath { 

    // Size of the cell 
    CGSize retval = CGSizeMake(130,130); 
    return retval; 
} 

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView 
         layout:(UICollectionViewLayout*)collectionViewLayout 
     insetForSectionAtIndex:(NSInteger)section { 

    //top, left, bottom, right 
    return UIEdgeInsetsMake(0, 20, 0, 20); 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
      minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { 

    //Minimum spacing between cells 
    CGFloat interimSpacing = 0.0f; 
    return interimSpacing; 
} 

- (CGFloat)collectionView:(UICollectionView *)collectionView 
        layout:(UICollectionViewLayout *)collectionViewLayout 
      minimumLineSpacingForSectionAtIndex:(NSInteger)section { 

    // Minimum spacing between lines. In your case, 
     this is the distance between columns 
    // If your scroll direction was vertical, 
     this would be the distance between rows 
    CGFloat lineSpacing = 20.0f; 
    return lineSpacing; 
} 

我已經預先填充了一些值,應該相當接近你想要完成的值。

或者,所有這些都可以在界面構建器中完成,方法是選擇UICollectionView並調整Size Inspector中的值,儘管在我看來這更令人困惑。

+0

另外,我想說明的是,如果你實現'',你不需要'',因爲前者是後者的子類。 – jhilgert00

+0

這樣做!感謝您的幫助,我同意在IB做這件事對我來說有點麻煩。 –

+0

@ jhilgert00不是一個子類,因爲這是一個協議。佈局協議也「符合」集合視圖委託,一個子類是完全不同的。 – Mundi