hzwzw hzwzw - 2 years ago 140
iOS Question

how to get the instance of the decoration view in the collection view?

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!

Answer Source

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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download