layoutAttributesForSupplementaryViewOfKind未调用

时间:2014-08-19 20:44:08

标签: ios uicollectionview

我正在为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;
}

所以即使这样,也不会调用补充视图的两种方法。

1 个答案:

答案 0 :(得分:11)

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

  

子类必须覆盖此方法,并使用它返回视图与指定矩形相交的所有项的布局信息。您的实现应返回所有可视元素的属性,包括单元格,补充视图和装饰视图。