1

有没有什么方法可以定制UICollectionViewFlowLayout,没有UICollectionViewDelegateFlowLayout methods

并且可以解析UICollectionViewLayoutAttributessize定的,而不给定的origin

所以我不需要UICollectionViewLayoutAttributes- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect方法中实现 per 的框架。

不计算每个项目的来源会很方便。

这是我的示例代码:

// call the two layout calculating methods , and combine.
- (void)prepareLayout{
    [super prepareLayout];
    if(self.attributesArray.count>0){
        [self.attributesArray removeAllObjects];
    }
    NSInteger indexCount = [self.collectionView numberOfItemsInSection: 0];
    for (NSInteger i = 0; i<indexCount; i++) {
        NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection: 0];
        UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: indexPath];
        [self.attributesArray addObject: attributes];
    }
    UICollectionViewLayoutAttributes * headAttributes = [self layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader atIndexPath: [NSIndexPath indexPathForItem:0 inSection: 0]];
    [self.attributesArray addObject: headAttributes];
}

// calculate the items layout
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes * cellAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: indexPath];
    CGFloat baseOriginY = 133;
    switch (indexPath.item) {
        case 0:
            {
                cellAttributes.frame = CGRectMake(0, baseOriginY, KScreenWidth, 50);
            }
            break;
        case 6: //7:
        case 7: //8:
            {
                NSInteger count = indexPath.item-6;
                cellAttributes.frame = CGRectMake(0, 155 + 50*count + baseOriginY, KScreenWidth, 50);
            }
            break;
        case 5: //6:
            {
                cellAttributes.frame = CGRectMake(0, 145 + baseOriginY, KScreenWidth, 10);
            }
            break;
        default:
            {
                NSInteger i = indexPath.item - 1;
                cellAttributes.frame = CGRectMake(i*KScreenWidth/4.0, 50 + baseOriginY, KScreenWidth/4.0, 95);
            }
            break;
    }
    return cellAttributes;
}

// calculate the headers layout
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewLayoutAttributes * headerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader withIndexPath: indexPath];
  //  headerAttributes.size = CGSizeMake(KScreenWidth, 133);
    headerAttributes.frame = CGRectMake(0, 0, KScreenWidth, 133);
    return headerAttributes;

}

// return the final layout results
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attributesArray;
}

换句话说,使用 的动态调整特性UICollectionViewFlowLayout,而不符合UICollectionViewDelegateFlowLayout协议。

正如 Apple 所说,动态调整功能:

流式布局在一个方向上使用固定距离,在另一个方向上使用可滚动距离来布置其内容。例如,在垂直滚动的网格中,网格内容的宽度被限制为相应集合视图的宽度,而内容的高度会动态调整以匹配网格中的部分和项目的数量。

我觉得很有可能,我现在不知道如何实现。也许是苹果的 API。

非常欢迎任何建议或解释。

提前谢谢了。

4

0 回答 0