自适应高度的表格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
10332 123
iOS 调试:通过 Safari/Chrome 调试 WebView
|
Web App开发 JavaScript Android开发
iOS SFSafariViewController 获取 Cookies
iOS SFSafariViewController 获取 Cookies
416 0
|
JSON 缓存 Android开发
iOS高质量的动画实现解决方案——Lottie
iOS高质量的动画实现解决方案——Lottie
1542 0
|
9月前
|
存储 Web App开发 缓存
清理C盘空间的6种方法,附详细操作步骤
释放C盘空间并不难。只要掌握合适的方法,哪怕你是电脑小白,也能轻松清理出几十GB空间。下面就为大家介绍6种实用、安全、细致的清理方法,并附上操作步骤。
|
Swift iOS开发
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
iOS 用一个布局来解决嵌套问题—— UICollectionViewCompositionalLayout
|
开发工具 Android开发 git
鸿蒙Flutter实战:01-搭建开发环境
本文介绍了如何搭建鸿蒙 Flutter 开发环境,包括安装 DevEco Studio 等工具,并详细说明了 Mac 和 Windows 系统下的环境变量配置。此外,还介绍了如何使用 FVM 管理多个 Flutter 版本,并提供了一些常见问题的解决方案和交流群信息。
645 0
鸿蒙Flutter实战:01-搭建开发环境
|
设计模式 负载均衡 监控
深入理解后端开发中的微服务架构
在现代软件开发领域,微服务架构已经成为一种流行的设计模式。本文将探讨微服务的基本概念、优势与挑战,并通过实例展示如何在实际项目中应用微服务架构。无论是初学者还是经验丰富的开发者,都能从中获得启发和实用技巧。
249 7
|
iOS开发 Perl
iOS Cocoapods 升级
iOS Cocoapods 升级
400 0
|
缓存 移动开发 JavaScript
WKWebView对网页和js,css,png等资源文件的缓存机制及如何刷新缓存
WKWebView对网页和js,css,png等资源文件的缓存机制及如何刷新缓存
1281 1
|
设计模式 测试技术
依赖注入与工厂设计模式的区别
【8月更文挑战第22天】
335 0

热门文章

最新文章