自适应高度的表格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实现控件根据内容进行宽度自适应和高度自适应》。

目录
相关文章
|
Swift iOS开发 开发者
iOS - 跳转App Store下载 app 的两种方式
iOS - 跳转App Store下载 app 的两种方式
3701 0
iOS - 跳转App Store下载 app 的两种方式
|
Web App开发 索引
正则表达式匹配域名、网址、url
DNS规定,域名中的标号都由英文字母和数字组成,每一个标号不超过63个字符,也不区分大小写字母。标号中除连字符(-)外不能使用其他的标点符号。级别最低的域名写在最左边,而级别最高的域名写在最右边。由多个标号组成的完整域名总共不超过255个字符。
32222 0
|
网络协议 Windows
59【工控通信】ModbusTCP通讯之ModbusPoll客户端工具配置
【工控通信】ModbusTCP通讯之ModbusPoll客户端工具配置
1709 0
|
Java Spring
动态控制 Spring Boot 中的 @Scheduled 定时任务
Spring Boot 中的 @Scheduled 注解为定时任务提供了一种很简单的实现,只需要在注解中加上一些属性,例如 fixedRate、fixedDelay、cron(最常用)等等,并且在启动类上面加上 @EnableScheduling 注解,就可以启动一个定时任务了。 但是在某些情况下,并没有这么简单,例如项目部署上线之后,我们可能会修改定时任务的执行时间,并且停止、重启定时任务等,因为定时任务是直接写死在程序中的,修改起来不是非常的方便。所以,简单记录一下自己的一些解决方案,仅供参考。
3132 0
|
存储 SQL 运维
Apache Doris 在小米统一 OLAP 和湖仓一体的实践
小米早在 2019 年便引入 Apache Doris 作为 OLAP 分析型数据库之一,经过五年的技术沉淀,已形成以 Doris 为核心的分析体系,并基于 2.1 版本异步物化视图、3.0 版本湖仓一体与存算分离等核心能力优化数据架构。本文将详细介绍小米数据中台基于 Apache Doris 3.0 的查询链路优化、性能提升、资源管理、自动化运维、可观测等一系列应用实践。
558 1
Apache Doris 在小米统一 OLAP 和湖仓一体的实践
|
iOS开发 Perl
iOS Cocoapods 升级
iOS Cocoapods 升级
553 0
|
Java 数据处理
技术分享:高效与灵活并存——Java版通用树形结构转换工具的实现与应用
在软件开发中,树形结构的数据表现形式无处不在,从文件系统的目录树到组织架构的部门树,再到各类产品的分类结构。处理这些具有层级关系的数据时,将其转换为树形结构以便展示和操作显得尤为重要。Java作为一门成熟的编程语言,虽然提供了强大的集合框架,但并未直接提供树形结构转换的内置工具。因此,开发一个高效且灵活的通用树形结构转换工具成为许多项目中的必备需求。
858 2
|
Java 编译器 测试技术
滚雪球学Java(04):JDK、IntelliJ IDEA的安装和环境变量配置
【2月更文挑战第11天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!!
789 1
UITableViewCell和UICollectionViewCell自适应高度
UITableView和UICollectionView想通,此处就已UITableView为例
595 0
UITableViewCell和UICollectionViewCell自适应高度
|
Java 数据库连接 mybatis
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)