iOS UICollectionViewCell 高度自适应

效果图:
048550D0-0870-4028-A950-DC682DBED6F6

cell的代码:

- (void) setupUI {
    self.backgroundColor = [QSKit color:@"#ffffff"];
    
    UILabel* nameLabel = [UILabel new];
    nameLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    nameLabel.textColor = [QSKit color:@"#444444"];
    [self.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.left.mas_equalTo(18);
    }];
    self.nameLabel = nameLabel;
    
    UILabel* phoneLabel = [UILabel new];
    phoneLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    phoneLabel.textColor = [QSKit color:@"#444444"];
    [self.contentView addSubview:phoneLabel];
    [phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(18);
        make.right.mas_equalTo(-18);
    }];
    self.phoneLabel = phoneLabel;
    
    UILabel* addressLabel = [UILabel new];
    addressLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    addressLabel.textColor = [QSKit color:@"#888888"];
    addressLabel.numberOfLines = 0;
    [self.contentView addSubview:addressLabel];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(18);
        make.right.mas_equalTo(-18);
    }];
    self.addressLabel = addressLabel;
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    UIView* collectionView = self.superview;
    if ([collectionView isKindOfClass:[UICollectionView class]]) {
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.width.mas_equalTo(collectionView.bounds.size.width);
            make.bottom.mas_equalTo(self.addressLabel.mas_bottom).offset(15);
        }];
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    CGSize size = [self.contentView systemLayoutSizeFittingSize:layoutAttributes.size];
    CGRect cellFrame = layoutAttributes.frame;
    cellFrame.size.height = size.height;
    layoutAttributes.frame = cellFrame;
    
    return layoutAttributes;
}
UICollectionViewFlowLayout* collectionLayout = [UICollectionViewFlowLayout new];
    collectionLayout.minimumLineSpacing = 12;
    collectionLayout.minimumInteritemSpacing = 0;
    collectionLayout.estimatedItemSize = CGSizeMake(600, 40);

注意:

collectionLayout.estimatedItemSize = CGSizeMake(600, 40);

没有这句是不会实现自适应的效果。