2012-11-28 80 views

答えて

2

私はこれをまだ試していませんが、書籍のアプリの棚のような背景が欲しい場合は、デコレーションビューを使用する必要があります。私はあなたが各セクションについて異なるビューを持つことができると思うし、デリゲートメソッドlayoutAttributesForDecorationViewOfKind:atIndexPath:を使用してそれらを設定する必要があります。

0

コレクションビューでは、すべてのセクションに補助ビューを設定できるため、セクションごとに補足ビューを配置し、セクションセルではなく補助ビューにバックグラウンドカラーを設定します。私はそれが助けてくれることを願う

+0

補助ビューについて読むと、ヘッダーとフッターに使用されている場合にのみ例が見つかります。あなたはあなたの答えのための例や作業コードを持っていますか? –

+0

私はコレクションビューのカスタムレイアウトを使用してそれを行っています。カスタムレイアウトでは、私の補足ビューフレームはそのセクションフレーム全体に等しい。 – Jirune

+1

あなたはどうしたのですか?もしあなたがそれが素晴らしいと言うことができるなら。ありがとう –

2

これはUICollectionView部分の色を変更するための素晴らしいチュートリアルです:

ユーザー@casamiaのおかげ

http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html

は基本的に、私たちは順番にUICollectionViewLayoutAttributesUICollectionReusableViewUICollectionViewLayoutをサブクラス化する必要があります更新リンク、セクションのバックグラウンドビューとしてインスタンスUICollectionReusableViewを作成します。

これは彼の結果である:

enter image description here

詳細説明へのリンクに従ってください。

+0

添付したリンクが変更されました。 http://www.ericjerican.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html – casamia

+3

そして@casamiaによって提供されるリンクも死んでいます。少なくともgithubプロジェクトへのリンクは次のとおりです。https://github.com/ericchapman/ios_decoration_view – TylerJames

-2

私は次の方法で非常に簡単な方法で各セクションの背景色を変更しました。しかし、それが正しいことであるかどうかはわかりませんでした。しかし、それはうまくいった。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    FamilyCalendarCellItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarItem" forIndexPath:indexPath]; 
    Event *event; 
    _headerView = [collectionView dequeueReusableSupplementaryViewOfKind: 
        UICollectionElementKindSectionHeader withReuseIdentifier:@"EventHeader" forIndexPath:indexPath]; //headerView is declared as property of Collection Reusable View class 
    if(indexPath.section==0) { 

     cell.backgroundColor=[UIColor orangeColor]; 
     } 
    else if(indexPath.section==1) { 

     cell.backgroundColor=[UIColor yellowColor]; 

    } 

    return cell; 
} 
+0

これは、セルの背景色のみを変更します。問題は、特定のセクション内のコレクションビュー自体の背景色を変更する方法を尋ねることです – TylerJames

0

アイデアは、色の属性を追加するUICollectionViewLayoutAttributesをオーバーライドすることです。 UICollectionReusableViewをオーバーライドし、ビューの背景に色を適用します。

https://github.com/strawberrycode/SCSectionBackground

0

私はスウィフト3

サブクラスuicollectionreusableview

class SectionView: UICollectionReusableView { 
    static let kind = "sectionView" 
} 

サブクラスuicollectionViewFlowLayout

ここでは、このレポのオフ https://github.com/SebastienMichoy/CollectionViewsDemo/tree/master/CollectionViewsDemo/Sources/Collections%20Views

を行ってきました

class CustomFlowLayout: UICollectionViewFlowLayout { // MARK: Properties var decorationAttributes: [IndexPath: UICollectionViewLayoutAttributes] var sectionsWidthOrHeight: [IndexPath: CGFloat] // MARK: Initialization override init() { self.decorationAttributes = [:] self.sectionsWidthOrHeight = [:] super.init() } required init?(coder aDecoder: NSCoder) { self.decorationAttributes = [:] self.sectionsWidthOrHeight = [:] super.init(coder: aDecoder) } // MARK: Providing Layout Attributes override func layoutAttributesForDecorationView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? { return self.decorationAttributes[indexPath] } override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { var attributes = super.layoutAttributesForElements(in: rect) let numberOfSections = self.collectionView!.numberOfSections var xOrYOffset = 0 as CGFloat for sectionNumber in 0 ..< numberOfSections { let indexPath = IndexPath(row: 0, section: sectionNumber) let numberOfItems = self.collectionView?.numberOfItems(inSection: sectionNumber) let sectionWidthOrHeight = numberOfItems == 0 ? UIScreen.main.bounds.height : collectionViewContentSize.height//self.sectionsWidthOrHeight[indexPath]! let decorationAttribute = UICollectionViewLayoutAttributes(forDecorationViewOfKind: SectionView.kind, with: indexPath) decorationAttribute.zIndex = -1 if self.scrollDirection == .vertical { decorationAttribute.frame = CGRect(x: 0, y: xOrYOffset, width: self.collectionViewContentSize.width, height: sectionWidthOrHeight) } else { decorationAttribute.frame = CGRect(x: xOrYOffset, y: 0, width: sectionWidthOrHeight, height: self.collectionViewContentSize.height) } xOrYOffset += sectionWidthOrHeight attributes?.append(decorationAttribute) self.decorationAttributes[indexPath] = decorationAttribute } return attributes } } 

は、レイアウトとUICollectionReusableViewないcollectionViewを登録

func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) { 

    Log.printLog(identifier: elementKind, message: indexPath) 
    if elementKind == UICollectionElementKindSectionHeader, let view = view as? ProfileViewHeaderView { 

     view.backgroundColor = UIColor(red: (102/255.0), green: (169/255.0), blue: (251/255.0), alpha: 1) 
    } else if elementKind == SectionView.kind { 
     let evenSectionColor = UIColor.black 
     let oddSectionColor = UIColor.red 

     view.backgroundColor = (indexPath.section % 2 == 0) ? evenSectionColor : oddSectionColor 
    } 
} 

これは重要です

let layout = CustomFlowLayout() 
layout.register(SectionView.self, forDecorationViewOfKind: SectionView.kind) 

をこの

CollectionViewデリゲートの機能を実装しています。

もう1つ。私はlayoutAttributesForElementsの高さを使いこなしました。あなた自身のプロジェクトのためにそれを変更する必要があります。

関連する問題