自适应高度的表格UICollectionView

简介: 自适应高度的表格UICollectionView

自适应高度的表格UICollectionView

1.设置预估高度:flowLayout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH-0*2), (50));//UICollectionViewFlowLayoutAutomaticSize;// 自适应 不设置预估高度表格显示不出来);

2.不设置表格高度;

3.表格设置为自适应高度(核心代码)。

contentL.preferredMaxLayoutWidth = SCREEN_WIDTH-BaseSize(13)*8;
[contentL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(BaseSize(13));
make.left.mas_equalTo(noIndexLab.mas_right).offset(BaseSize(3));
make.right.mas_equalTo(-BaseSize(13));
make.bottom.mas_equalTo(-BaseSize(5));
}];

-(UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {
[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;
}

具体实现:表格代码

#import <UIKit/UIKit.h>
#import "QuestSelectCell.h"
#import "QuestDetailModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface QuestSelectCell : UICollectionViewCell

@property (weak, nonatomic) UILabel *noIndexLab, *contentLab;
@property (weak, nonatomic) UIButton *markBtn;
@property (nonatomic,strong) QuestOptionModel *optionModel ;
@property (nonatomic,copy) void (^seletBlock)(void) ;

@end
#import "QuestSelectCell.h"

@implementation QuestSelectCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIButton * btn = [UIButton buttonWithImage:@"round_normal" select:@"round_select"];
        [self.contentView addSubview:btn];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(BaseSize(13));
            make.width.height.mas_equalTo(BaseSize(19.2));
        }];
        self.markBtn = btn;
        
        UILabel *noIndexLab = [UILabel labelWithText:@"A." font:SystemFontOfSize(15) textColor:TextBlackColor];
        [self.contentView addSubview:noIndexLab];
        [noIndexLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(BaseSize(13));
            make.left.mas_equalTo(BaseSize(49));
            make.height.width.mas_equalTo(BaseSize(18));
        }];
        self.noIndexLab = noIndexLab;
        
        UILabel *contentL = [UILabel labelWithText:@"内容" font:SystemFontOfSize(15) textColor:TextBlackColor];
        contentL.numberOfLines = 0;
        contentL.preferredMaxLayoutWidth = SCREEN_WIDTH-BaseSize(13)*8;
        [self.contentView addSubview:contentL];
        [contentL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(BaseSize(13));
            make.left.mas_equalTo(noIndexLab.mas_right).offset(BaseSize(3));
            make.right.mas_equalTo(-BaseSize(13));
            make.bottom.mas_equalTo(-BaseSize(5));
        }];
        self.contentLab = contentL;
    }
    return self;
}

- (void)setOptionModel:(QuestOptionModel *)optionModel {
    
    _optionModel = optionModel;
    [self.markBtn setSelected:optionModel.isSelect];
    [self.markBtn setImage:optionModel.QType==1?[UIImage imageNamed:@"round_normal"]:[UIImage imageNamed:@"square_normal"] forState:UIControlStateNormal];
    [self.markBtn setImage:optionModel.QType==1?[UIImage imageNamed:@"round_select"]:[UIImage imageNamed:@"square_select"] forState:UIControlStateSelected];
    self.noIndexLab.text = [NSString stringWithFormat:@"%@.",optionModel.OptionVal] ;
    [self.contentLab setText:optionModel.OptionTxt];
    
}

- (void)btnClick {
    if (self.seletBlock) {
        self.seletBlock();
    }
}

-(UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {
    [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;
}


@end

UICollectionView构造类:

- (UICollectionView *)collectionView {
    if(!_collectionView)
    {
        //创建布局
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH-0*2), (50));//UICollectionViewFlowLayoutAutomaticSize;// 自适应
        //创建CollectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, FULL_HEIGHT -(kStatusBarHeight+ TABBAR_HEIGHT+18+5.0+40)) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = YES;
        _collectionView.alwaysBounceVertical = NO;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor clearColor];//RGBA(246, 246, 246, 1);//BGColorHex(F9F9F9);
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"lineFootView"];
//        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        [_collectionView registerClass:[FHSchemeCell class] forCellWithReuseIdentifier:NSStringFromClass([FHSchemeCell class])];
        [_collectionView registerClass:[FHImageTextCell class] forCellWithReuseIdentifier:NSStringFromClass([FHImageTextCell class])];
//        [_collectionView registerClass:[BGCommonDescribeInputCell class] forCellWithReuseIdentifier:NSStringFromClass([BGCommonDescribeInputCell class])];
//        [_collectionView registerClass:[DYAddressDescribeInputCell class] forCellWithReuseIdentifier:NSStringFromClass([DYAddressDescribeInputCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeadView"];
        
        
//        //定义每个UICollectionView 的大小
//        flowLayout.itemSize = CGSizeMake((kUIScreenWidth -COMMON_EDGE_DISTANCE*2) , 310);
//        //定义每个UICollectionView 横向的间距
//        flowLayout.minimumLineSpacing = 10;
//        //定义每个UICollectionView 纵向的间距
//        flowLayout.minimumInteritemSpacing = 10;
        //定义每个UICollectionView 的边距距
        //    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//上左下
        @weakify(self);
        [self.collectionView addImmediatelyRefreshHandler:^{
            @strongify(self);
//            self.firstExcuteHome_personalCommand = NO;
            [self headerRefresh];

        } completeHandler:^{
            @strongify(self);

        }];
        
        [self.collectionView addBITPullToLoadMoreWithActionHandler:^{
            @strongify(self);
//            self.firstExcuteHome_personalCommand = NO;
            [self footerRefresh];
        }];
        self.collectionView.showsBITPullToLoadMore = NO;
    }
    return _collectionView;
}

不要实现表格高度:

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;// 自适应
    return UICollectionViewFlowLayoutAutomaticSize;
    if(indexPath.row < 3)
    {
        return CGSizeMake(kUIScreenWidth, 45);
    }
//
//}

实现自适应高度和宽度见《使用Masonry实现控件(包括UITableView)根据内容进行宽度自适应和高度自适应》《使用SDAutoLayout实现控件根据内容进行宽度自适应和高度自适应》

目录
相关文章
|
Web App开发 Android开发 iOS开发
iOS 调试:通过 Safari/Chrome 调试 WebView
iOS 调试:通过 Safari/Chrome 调试 WebView
8819 0
iOS 调试:通过 Safari/Chrome 调试 WebView
|
移动开发 Java 测试技术
iOS的App模块化编程的框架实现方案——BeeHive
BeeHive是用于iOS的App模块化编程的框架实现方案,吸收了Spring框架Service的理念来实现模块间的API耦合。
iOS的App模块化编程的框架实现方案——BeeHive
|
JSON 缓存 Android开发
iOS高质量的动画实现解决方案——Lottie
iOS高质量的动画实现解决方案——Lottie
1394 0
|
7月前
|
Dart 开发工具 Android开发
在macOS系统上配置Flutter环境的步骤
在macOS系统上配置Flutter环境的步骤
881 62
|
8月前
|
JSON 数据格式
基于 PAI-ArtLab 使用 ComfyUI 生成服装面料迁移
本文介绍了通过PAI ArtLab平台将服装印花迁移到模特穿上的实验过程。首先,用户需登录阿里云并完成实名认证,访问PAI ArtLab平台领取免费试用资源。接着,在ComfyUI(专享版)中加载Json工作流文件,上传衣服版型、印花和模特图片,添加提示词队列等待生成结果。最后,提供了常见问题解答,如语言切换和任务进度查看方法。
|
9月前
|
存储 开发工具 数据安全/隐私保护
iOS各个证书生成细节
在iOS开发中,证书和标识符的配置至关重要。CSR(证书签名请求)由私钥生成,提交给苹果获取`.cer`证书。P12文件由`.cer`证书和私钥导出,用于开发环境。App ID确保应用唯一性。流程包括创建App ID、生成CSR、申请证书并导出P12。主P12和推送P12可使用不同CSR。
417 3
|
Swift iOS开发
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
手势代理 shouldBeRequiredToFailByGestureRecognizer 和 shouldRequireFailureOfGestureRecognizer 的区别
手势代理 shouldBeRequiredToFailByGestureRecognizer 和 shouldRequireFailureOfGestureRecognizer 的区别
407 10
|
监控 数据挖掘 数据安全/隐私保护
ERP系统中的绩效管理与考核
【7月更文挑战第25天】 ERP系统中的绩效管理与考核
598 2
|
设计模式 测试技术
依赖注入与工厂设计模式的区别
【8月更文挑战第22天】
269 0