2016-07-15 42 views
5

無論iPhone的屏幕尺寸是多少,我都想只顯示一行中的兩個單元格。像, End result required只顯示兩列,在一個CollectionView中使用故事板中的多行

我的故事板包含一個UICollectionView,通過約束連接。

Storyboard preview

UICollectionView故事板的設置是, enter image description here

現在,當我在6,5S或以下運行此,只有一個小區在一行中出現。我已經使用的代碼,

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
    return [categoryImages count]; 
} 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath]; 
    cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row]; 

    return cell; 
} 

我試圖檢測屏幕尺寸和使用的代碼編程分配合適的單元尺寸,

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


    CGSize result = [[UIScreen mainScreen] bounds].size; 
    NSLog(@"%f",result.height); 
    if(result.height == 480) 
    { 
     //Load 3.5 inch xib 
     sizes =CGSizeMake(170.f, 170.f); 
     NSLog(@"3gs"); 
    } 
    else if(result.height == 568) 
    { 
     //Load 4 inch xib 
     sizes =CGSizeMake(130.f, 130.f); 
     NSLog(@"5s"); 

    } 
    else if(result.height == 667.000000) 
    { 
     //Load 4.7 inch xib 
     sizes =CGSizeMake(160.f, 160.f); 
     NSLog(@"6"); 

    } 
    else 
    { 
     NSLog(@"else"); 

     sizes =CGSizeMake(170.f, 165.f); 

    } 
    return sizes; 
} 

但我也知道,這是不正確的做法,所以請給我一個正確的方式來處理這個問題。

回答

18

您可以這樣做,您不需要檢查設備尺寸,因爲我們可以使用tableView寬度來計算單元格寬度&,根據該寬度我們可以調整高度。

你需要使用CollectionViewFlowLayout &確認委託&實現下

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    let padding: CGFloat = 50 
    let collectionViewSize = collectionView.frame.size.width - padding 

    return CGSizeMake(collectionViewSize/2, collectionViewSize/2) 

} 

更新方法一兩件事:雨燕4.0

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let padding: CGFloat = 50 
     let collectionViewSize = collectionView.frame.size.width - padding 

     return CGSize(width: collectionViewSize/2, height: collectionViewSize/2) 
    } 

注意:我已經回答了考慮的問題細胞正方形大小

+0

感謝您節省我的一天。我很愚蠢,我甚至沒有想到這個簡單的解決方案。非常感謝! –

+0

這個答案真的可以幫助我...用戶可以根據需要更改填充... –

+0

這真的有所幫助。感謝您的回答和您提出的問題。 –

1
- (CGSize)collectionView:(UICollectionView *)collectionView 
       layout:(UICollectionViewLayout *)collectionViewLayout 
sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
CGFloat padding = 50; 
CGFloat cellSize = collectionView.frame.size.width - padding; 
return CGSizeMake(cellSize/2, cellSize/2); 
} 
8

的Xcode 8:斯威夫特3

我知道這是一個老問題,但我發現接受的答案的幾個問題。

我不得不使用UICollectionViewDelegateFlowLayout這是不CollectionViewFlowLayout

然後

在其「填充」的類型爲int和當您嘗試從變量collectionViewSize減去它,它拋出一個錯誤因爲它們是不同的類型。但很容易修復。

我只是說:CGFloat的到線

最後,雖然有可能是一個更好的辦法來做到這一點,我不得不調整的CollectionView開頭和結尾的約束填充匹配。

因此,所有的這一切都是我的代碼結束什麼看起來像

extension LandingPageViewController: UICollectionViewDelegateFlowLayout { 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    let padding: CGFloat = 25 
    let collectionCellSize = collectionView.frame.size.width - padding 

    return CGSize(width: collectionCellSize/2, height: collectionCellSize/2) 

    } 

} 
0

我與的CollectionView的邊界問題傳遞到sizeForItemAtIndexPath不是在那個時刻是正確的。

對此的一個修復方法是重新加載viewWillAppear中的單元格以確保collectionView的邊界是正確的。

另一個是使用UIScreen類。

-(instancetype)init: { 
    self = [super init]; 
    if (self) { 
     width = [[UIScreen mainScreen] bounds].size.width; 
    } 
    return self; 
} 

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

    // Two columns with spacing in between 
    CGFloat size = ((width/2) - (kSpacing * 2)); 
    return CGSizeMake(size, size); 

} 

在這種情況下,我實例化委託作爲一個單獨的類(因此INIT),但它可以很容易地完成作爲一個擴展。

相關問題