我正在使用自定义 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 。向其传递数据很复杂。
- 创建
UICollectionViewLayoutAttributes
的自定义子类保存数据。 - 在您的
UICollectionViewLayout
中子类,覆盖layoutAttributesClass
返回您的自定义属性类。 - 也在您的
UICollectionViewLayout
中子类,在layoutAttributesForElementsInRect:
中正确设置装饰 View 的属性和layoutAttributesForDecorationViewOfKind:atIndexPath:
. - 在您的装饰 View 类 (
noContentDecoration
) 中,覆盖applyLayoutAttributes:
从属性对象中提取数据并应用它们。
这看起来太复杂了吗?也许您最好使用补充 View 而不是装饰 View 。如果您使用补充 View ,则步骤如下:
- 在您的
UICollectionViewDataSource
中(这可能是你的 View Controller ),实现collectionView:viewForSupplementaryElementOfKind:atIndexPath:
获取 View (使用dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
),然后使用数据设置它,然后返回它。
如果您需要使用模型中的数据对其进行自定义,我建议您使用补充 View 而不是装饰 View 。
关于ios - 将数据传递给 UICollectionReusableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20945793/