1

我正在使用 UICollectionViewCompositionalLayout 创建网格。每个单元格包含一个多行标签。我的目标是,当组/行中的一项增长时,该组/行中的所有其他项目都增长到相同的高度,例如在所附图片中,我希望第 2 行第 2 列和第 3 列中的单元格匹配包含 ("1000\ndetail") 的第一个单元格的高度。 在此处输入图像描述

我尝试了各种方法,例如将项目的高度设置为小数(1),但这会阻止多行。其他尝试(例如更改小组规模)也无济于事。请问如何设置我的 UICollectionViewCompositionalLayout 来实现我的目标?

收藏视图:

NSCollectionLayoutSize *itemSize = [NSCollectionLayoutSize sizeWithWidthDimension:[NSCollectionLayoutDimension fractionalWidthDimension:0.333] heightDimension:[NSCollectionLayoutDimension fractionalHeightDimension:1]];
NSCollectionLayoutItem *item = [NSCollectionLayoutItem itemWithLayoutSize:itemSize];

NSCollectionLayoutSize *groupSize = [NSCollectionLayoutSize sizeWithWidthDimension:[NSCollectionLayoutDimension fractionalWidthDimension:1] heightDimension:[NSCollectionLayoutDimension estimatedDimension:40]];
NSCollectionLayoutGroup *group = [NSCollectionLayoutGroup horizontalGroupWithLayoutSize:groupSize subitem:item count:3];

NSCollectionLayoutSection *section = [NSCollectionLayoutSection sectionWithGroup:group];
UICollectionViewCompositionalLayout *compositionalLayout = [[UICollectionViewCompositionalLayout alloc] initWithSection:section];

UICollectionView *cv = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:compositionalLayout];
cv.translatesAutoresizingMaskIntoConstraints = NO;
cv.dataSource = self;
cv.delegate = self;
[self.view addSubview:cv];

[cv registerClass:[GridCVCell class] forCellWithReuseIdentifier:@"Cell"];

[cv.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:100].active = YES;
[cv.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:0].active = YES;
[cv.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:0].active = YES;
[cv.heightAnchor constraintEqualToConstant:300].active = YES;

集合视图单元

- (instancetype)initWithFrame:(CGRect)frame {
    
    if ([super initWithFrame:frame]) {
  
        UILabel *titleL = [UIlabel new];
        titleL.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:titleL];

        [titleL.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:10].active = YES;
        [titleL.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-10].active = YES;
        [titleL.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor constant:-2].active = YES;
        
        [self.contentView.heightAnchor constraintEqualToAnchor:titleL.heightAnchor constant:10].active = YES;
    }
    
    return self;
}
4

0 回答 0