17

UICollectionViewに2つのヘッダーがありますか?私はまた、ヘッダーとフッター持つフローレイアウトを使用していますUICollectionView、持って2つのヘッダーを持つUICollectionView

--------- 
| head1 | 
--------- 
| head2 | 
--------- 
| A | B | 
--------- 
| C | D | 
--------- 
| foot | 
--------- 
:時々

--------- 
| head | 
--------- 
| A | B | 
--------- 
| C | D | 
--------- 
| foot | 
--------- 

を、私はそうのような2つのヘッダを持っているしたいのですが

私はこれを達成する方法に固執しています。フローレイアウトは、1つの頭と1つの足を可能にするようにしか見えません。 2番目のヘッダーを追加するにはどうすればよいですか?


編集: - http://blog.radi.ws/post/32905838158/sticky-headers-for-uicollectionview-using - 私はまた、粘着性のヘッダーを実装しているが、私は最初のヘッダだけが粘着性になりたいです。このため、すべてを1つのヘッダーに含めることはできません。

答えて

34

単純なトリックを使用するだけです。すべてのセクションのヘッダーとフッターを両方とも表示する必要があります。

どのセクションでは、あなただけのそのサイズがゼロを渡すフッターを表示したくないとして: -

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section 
{ 
    if(section==0) 
    { 
     return CGSizeZero; 
    } 

    return CGSizeMake(320, 50); 
} 
ここ

私は

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

のように2つのセクションが使用されていないとの行の何に合格しています最後のセクションは1つだけです。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    if (section==1) { 
     return 20; 
    } 
    return 0; 
} 

ここに私の出力---

enter image description here

レッドビューは、ヘッダであるとグリーン一つはフッタです。

Here u can get the entire Implementation File

+0

これは良い方向性のようです。この方法では、2つのヘッダーに異なるレイアウトを使用できますか?そして、私が1つのヘッダーを「スティッキー」にし、もう1つをスクロールさせるようにします。 – cannyboy

+0

ええ、なぜ、単にviewForSupplementaryElementOfKindにセクション条件を適用すると、各セクションの異なるレイアウトのヘッダーを取得できます。 – Warewolf

+0

どのような答え.... @Herçules –

4

ヘッダー(1と2)の両方を別のビューに置き、ヘッダー1としてそのビューを配置する必要があります。したがって、コレクションビューのヘッダーにのみ作成します。

+0

なぜ私は2つのサブビューを持つことはできません説明するための答え。 – cannyboy

+0

まだこれは動作するはずです彼らが2つのヘッダーであるとは思わないでください。 – rckoenes

+1

ヘッダーの動作が異なるため、これがどのように機能するかは考えられません。 – cannyboy

2

ヘッダーを1つ追加していますか?セクションヘッダーを指定すると思いますか? 2つのヘッダーを持つレシピは、1つのヘッダーメインビュー内に2つのヘッダーサブビューを持つことです。

+0

2つのサブビューがうまくいかない理由を説明するために答えを更新しました。 – cannyboy

+0

両方のヘッダーサブビューで異なる自動サイズ変更マスクを試すことができます。 –

5

このコンテンツは、あなたが

クラスCollectionHeaderViewを作成し、それがUICollectionReusableViewから派生した容器を作るために作る、そして後の2のUIViewを作成し、このコンテナに

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionReusableView *reusableview = nil; 

    if (kind == UICollectionElementKindSectionHeader) { 
     CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; 

     headerView.firstContainer.titleLabel.text = @"MY Header View 1";//Here you can set title 

     headerView.secondContainer.titleLabel.text = @"MY Header View 2";//Here you can set title 
     UIImage *headerImage = [UIImage imageNamed:@"header_banner.png"]; 
     headerView.firstContainer.backgroundImage.image = headerImage; 
     headerView.secondContainer.backgroundImage.image = headerImage; 

     reusableview = headerView; 
    } 

    if (kind == UICollectionElementKindSectionFooter) { 
     UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath]; 

     reusableview = footerview; 
    } 

    return reusableview; 
} 
それを置きたいものを達成するためにあなたを助けるかもしれません
+0

このリンクは質問に答えるかもしれませんが、回答本体に必要な情報を投稿することをお勧めします。リンクのみの回答はお勧めできません。 – Krishnabhadra

+0

@Krishnabhadraオクラホマ私はこれを行った..ありがとう:) – SachinVsSachin

+0

さらに読むためにhttp://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Krishnabhadra

2

あなたができることは、2つのセクションを持つUITableViewを使用し、2つ目のセクションのセルにUICollectionViewを配置することです。

0

部が第二区間である場合にのみ、スティッキーヘッダを実装する、再利用可能なヘッダに二つのビューを作成することによって、この作業を得ました。また、numberOfSectionを2に調整します。viewForSupplementaryElementOfKindで表示を隠して表示することによってヘッダーを切り替えるようにしました。

verticalCollectionView.register(UINib(nibName: "OfferHeaderCell", bundle: nil), forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "OfferHeaderCell") 
verticalCollectionView.register(UINib(nibName: "APRHeaderCell", bundle: nil), forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "APRHeaderCell") 

その後、先に行くと、各セクションのヘッダーを返し、:

0

種類UICollectionElementKindSectionHeaderのあなたのヘッダー・ビューになりますあなたのUICollectionViewCellを定義 - OfferHeaderCellとAPRHeaderCell以下のように定義 - 私の場合は、2つのヘッダを持っています以下のように2つの異なるセクションのためviewForSupplementaryElementOfKindを定義する

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    if(section==0) { 
     return CGSize.zero 
    } else if (section==1) { 
     return CGSize(width:collectionView.frame.size.width, height:133) 
    } else { 
     return CGSize(width:collectionView.frame.size.width, height:100) 
    } 

} 

重要このUICollectionViewDelegateFlowLayout関数にゼロの大きさを有するようにセクションヘッダーのサイズを設定します。

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

    var reusableview = UICollectionReusableView() 
    if (kind == UICollectionElementKindSectionHeader) { 
     let section = indexPath.section 
     switch (section) { 
     case 1: 
      let firstheader: OfferHeaderCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OfferHeaderCell", for: indexPath) as! OfferHeaderCell 
      reusableview = firstheader 
     case 2: 
      let secondHeader: APRHeaderCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "APRHeaderCell", for: indexPath) as! APRHeaderCell 
      reusableview = secondHeader 
     default: 
      return reusableview 

     } 
    } 
    return reusableview 
} 

そして最後にデータソース、

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 3 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if (section==2) { 
     return 2 
    } 
    return 0 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = verticalCollectionView.dequeueReusableCell(withReuseIdentifier: "ReviseOfferCell", for: indexPath) 
    cell.backgroundColor = UIColor.white 
    return cell 
} 

注:

// MARK:以下のようにUICollectionFlowLayoutを追加するのを忘れないでください、私は更新されてきたUICollectionViewDelegateFlowLayout

extension MakeAnOfferController: UICollectionViewDelegateFlowLayout { 

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

      if indexPath.item == 0 { 
       return CGSize(width: self.view.frame.size.width, height: 626.0) 
      } 
      return CGSize() 
     } 

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 

      if(section==0) { 
       return CGSize.zero 
      } else if (section==1) { 
       return CGSize(width:collectionView.frame.size.width, height:133) 
      } else { 
       return CGSize(width:collectionView.frame.size.width, height:100) 
      } 
     } 
    } 
関連する問題