ios – layoutAttributesForSupplementaryViewOfKind未调用

前端之家收集整理的这篇文章主要介绍了ios – layoutAttributesForSupplementaryViewOfKind未调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为UICollectionView编写自定义流布局.
我可以看到并滚动细胞.

问题是我无法显示标题的补充视图.

所以,

- (UICollectionViewLayoutAttributes *)
      layoutAttributesForSupplementaryViewOfKind:(NSString *)kind 
      atIndexPath:(NSIndexPath *)indexPath

永远不会被召唤.

在数据源中这种方法

- (UICollectionReusableView *)
      collectionView:(UICollectionView *)collectionView 
      viewForSupplementaryElementOfKind:(NSString *)kind 
      atIndexPath:(NSIndexPath *)indexPath

也永远不会被召唤.

我怎样才能这样调用这些方法呢?

编辑:
这是layoutAttributesForElementsInRect:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSMutableArray * attributes = [NSMutableArray arrayWithCapacity:[[self.layoutInfo allKeys] count]];
    for(NSIndexPath * indexPath in self.layoutInfo) {
        UICollectionViewLayoutAttributes *itemAttributes = [self.layoutInfo objectForKey:indexPath];
        if(CGRectIntersectsRect(rect,itemAttributes.frame)) {
            [attributes addObject:itemAttributes];
        }
    }
    return attributes;
}

因此即使这样,也不会调用补充视图的两种方法.

解决方法

您需要实现 layoutAttributesForElementsInRect:以返回每个补充视图的属性实例(除了每个单元格):

Subclasses must override this method and use it to return layout information for all items whose view intersects the specified rectangle. Your implementation should return attributes for all visual elements,including cells,supplementary views,and decoration views.

猜你在找的iOS相关文章