草庐IT

ios - layoutAttributesForSupplementaryViewOfKind 未调用

coder 2023-09-20 原文

我正在为 UICollectionView 编写自定义流布局。 我可以看到并滚动单元格。

问题是我无法显示节标题的补充 View 。

所以,

- (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;
}

因此即使这样,也不会调用补充 View 的两个方法。

最佳答案

您需要实现 layoutAttributesForElementsInRect:为每个补充 View (除了每个单元格)返回一个属性实例:

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 - layoutAttributesForSupplementaryViewOfKind 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25392633/

有关ios - layoutAttributesForSupplementaryViewOfKind 未调用的更多相关文章

随机推荐