ios - 将数据传递给 UICollectionReusableView

标签 ios objective-c uicollectionview

我正在使用自定义 UICollectionViewFlowLayout,并且我已为此布局注册了自定义可重用类...

 [self registerClass:[noContentDecoration class] forDecorationViewOfKind:knoContentsDecorationKind];

现在,我想在初始化阶段将一些数据传递给这个装饰 View ,我想在这个可重用类中设置一些属性

但是我无法到达这个 View ,我只找到了 UICollectionView 的委托(delegate)方法:

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath

但这里的问题是:仅当可重用 View 被删除时才会调用此方法,如文档中所述:

Use this method to detect when a supplementary view is removed from a collection view, as opposed to monitoring the view itself to see when it appears or disappears.

我可以做什么来在初始化中将数据传递到这个可重用 View ?

谢谢

最佳答案

您正在使用装饰 View 。向其传递数据很复杂。

  1. 创建 UICollectionViewLayoutAttributes 的自定义子类保存数据。
  2. 在您的UICollectionViewLayout中子类,覆盖layoutAttributesClass返回您的自定义属性类。
  3. 也在您的 UICollectionViewLayout 中子类,在 layoutAttributesForElementsInRect: 中正确设置装饰 View 的属性和layoutAttributesForDecorationViewOfKind:atIndexPath: .
  4. 在您的装饰 View 类 ( noContentDecoration ) 中,覆盖 applyLayoutAttributes:从属性对象中提取数据并应用它们。

这看起来太复杂了吗?也许您最好使用补充 View 而不是装饰 View 。如果您使用补充 View ,则步骤如下:

  1. 在您的UICollectionViewDataSource中(这可能是你的 View Controller ),实现 collectionView:viewForSupplementaryElementOfKind:atIndexPath:获取 View (使用 dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: ),然后使用数据设置它,然后返回它。

如果您需要使用模型中的数据对其进行自定义,我建议您使用补充 View 而不是装饰 View 。

关于ios - 将数据传递给 UICollectionReusableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945793/

相关文章:

ios - 解析查询结果未附加到新数组

ios - 来自 UICollectionViewCell 中的 Class 对象的 UITableView 数据源

iphone - 本地通知 "didReceiveLocalNotification"调用两次

ios - 如何在for循环中调用web服务?

ios - 淡出循环动画

iphone - 关于 UIView 的 setNeedsDisplay 的困惑

iphone - UICollectionView:如何获取部分的标题 View ?

ios - 在 UICollectionViewController 中仅选择一项

iOS - 是否可以在设备旋转时仅旋转导航栏按钮?

ios - 我怎么可能在我的 NSMutableDictionary 中得到重复的键?