2

I've implemented an UICollectionView with a custom layout. I know how to present visual adornments for the entire collection view, but don't know how to present different image for every section. I need to get the instance of decoration view by indexPath. I found this on Apple Document:

Use this method to create a layout attributes object for a decoration view in the collection view. Decoration views are a type of supplementary view but do not present data that is managed by the collection view’s data source. Instead, they mostly present visual adornments for a section or for the entire collection view.

It is up to you to decide how to use the indexPath parameter to identify a given decoration view. Typically, you use the decorationViewKind parameter to identify the type of the decoration view and the indexPath information to distinguish between different instances of that view.

who can tell me how to do it in detail?

thanks a lot!

hzwzw
  • 1,042
  • 12
  • 17
  • Please include the code where you currently load the content for each collection view cell. – Brian Shamblen Apr 27 '15 at 23:52
  • There's a great tutorial that covers this ... it's a little dated but still very relevant. Look at http://skeuo.com/uicollectionview-custom-layout-tutorial – MDB983 Apr 28 '15 at 00:07
  • Thanks man!I just follow the tutorial online: [link](http://stackoverflow.com/questions/12810628/uicollectionview-decoration-view). actually, all the solutions are same.@BrianShamblen – hzwzw Apr 28 '15 at 00:19
  • Thanks man! I downloaded the source code in the link that you show to me! and read it carefully! but i still can't find the way to solve my problem. I want to get the decoration view's instance and set different background image for each section's decoration view. @MDB983 – hzwzw Apr 28 '15 at 00:34
  • in your custom Layout you can respond to: (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath or - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath and set the background accordingly. The indexPath contains the Section – MDB983 Apr 28 '15 at 00:43
  • thanks for your patience! but does there have any property named backgroud image or color in UICollectionViewLayoutAttributes class?@MDB983 – hzwzw Apr 28 '15 at 01:07

1 Answers1

10

Answer my own question! Thanks for this article!

First, subclass UICollectionViewLayoutAttributes and add any property you want.

Second, in the function layoutAttributesForDecorationViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes!, set the property.

Third, in the UICollectionReusableView class, override the function applyLayoutAttributes: to get the custom UICollectionViewLayoutAttributes instance that contains your property.

Done!

hzwzw
  • 1,042
  • 12
  • 17
  • @Pete Thanks man! I appreciate your help in correcting my spelling error!. – hzwzw Jun 16 '16 at 01:33
  • 1
    @vmeyer Thanks for the reminder. The author probably changed his blog's structure. I have fixed it. – hzwzw Jan 07 '17 at 11:48