2016-07-15 6 views
5

iPhoneの画面サイズに関係なく、行内に2つのセルしか表示しません。 Like、 End result requiredストーリーボードを使用してCollectionView内に複数の行だけを表示する

私のストーリーボードには、制約によって接続されたUICollectionViewが含まれています。私は6、5S以下でこれを実行すると

Storyboard preview

UICollectionViewためのストーリーボードの設定は、今、 enter image description here

である、唯一の1つのセルが行に表示されます。私が使用したのだコードは、私が

- (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; 
} 
、画面サイズを検出し、コードを使用してプログラムの適切なセルサイズを割り当ててみました。しかし、私はまた、これは正しい方法ではないことを知っている、

- (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; 
} 

ですですから、これを処理する正しい方法を教えてください。

答えて

18

tableViewの幅を使用してセルの幅を計算して、&の高さを上げることができるので、デバイスサイズを確認する必要はありません。スウィフト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) 
    } 

::私は質問を考慮に答えているあなたは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) 

} 

更新以下のメソッドを実装確認します細胞は正方形のサイズである

+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

スウィフト私は、これは古い質問ですけど、私は受け入れ答えを持ついくつかの問題を発見しました。

私は「パディング」タイプのintでCollectionViewFlowLayout

その後

それで

ないUICollectionViewDelegateFlowLayoutを使用していたとは、変数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の境界が正しいことを確認します。

もう1つは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); 

} 

Iは、別のクラス(したがってINIT)としてデリゲートをインスタンス化していますが、それは同じように簡単に拡張として行うことができます。

関連する問題