【问题标题】:how to get the instance of the decoration view in the collection view?如何在集合视图中获取装饰视图的实例?
【发布时间】:2015-04-27 23:16:24
【问题描述】:

我已经实现了一个带有自定义布局的 UICollectionView。我知道如何为整个集合视图呈现视觉装饰,但不知道如何为每个部分呈现不同的图像。我需要通过 indexPath 获取装饰视图的实例。我在 Apple Document 上找到了这个:

使用此方法为集合视图中的装饰视图创建布局属性对象。装饰视图是一种补充视图,但不呈现由集合视图的数据源管理的数据。相反,它们主要为部分或整个集合视图呈现视觉装饰。

由您决定如何使用 indexPath 参数来识别给定的装饰视图。通常,您使用 decorationViewKind 参数来标识装饰视图的类型,并使用 indexPath 信息来区分该视图的不同实例。

谁能详细告诉我怎么做?

非常感谢!

【问题讨论】:

  • 请包含您当前加载每个集合视图单元格内容的代码。
  • 有一个很棒的教程涵盖了这个......它有点过时但仍然非常相关。看skeuo.com/uicollectionview-custom-layout-tutorial
  • 谢谢大佬!我只是按照在线教程进行操作:link。实际上,所有的解决方案都是一样的。@BrianShamblen
  • 谢谢你!我在您显示给我的链接中下载了源代码!并仔细阅读!但我仍然找不到解决问题的方法。我想获取装饰视图的实例并为每个部分的装饰视图设置不同的背景图像。 @MDB983
  • 在您的自定义布局中,您可以响应:(UICollectionViewLayoutAttributes )layoutAttributesForDecorationViewOfKind:(NSString)decorationViewKind atIndexPath:(NSIndexPath *)indexPath 或 - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 并相应地设置背景。 indexPath 包含 Section

标签: ios uicollectionview


【解决方案1】:

回答我自己的问题! 感谢this article

首先,继承UICollectionViewLayoutAttributes 并添加您想要的任何属性。

其次,在函数layoutAttributesForDecorationViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes!中,设置属性。

第三,在UICollectionReusableView 类中,重写函数applyLayoutAttributes: 以获取包含您的属性的自定义UICollectionViewLayoutAttributes 实例。

完成!

【讨论】:

  • @Pete 谢谢老兄!感谢您帮助纠正我的拼写错误!
  • @vmeyer 感谢您的提醒。作者可能改变了他的博客结构。我已经修好了。
猜你喜欢
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 2019-02-12
  • 2014-02-04
相关资源
最近更新 更多