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

目录
相关文章
|
6月前
UITableView根据表格内容进行高度自适应与使用Masonry实现根据内容进行宽度自适应和高度自适应
UITableView根据表格内容进行高度自适应与使用Masonry实现根据内容进行宽度自适应和高度自适应
85 0
UITextView设置边框
UITextView设置边框
61 0
UICollectionView的布局
UICollectionView的布局
388 0
UICollectionView的布局
|
图形学
控件渐变色的实现
控件渐变色的实现(一)—— CAGradientLayer实现控件渐变色的实现(二)—— Core Graphics实现
785 0
|
C#
C# 根据Combobox控件来动态显示TabControl下的子元素
  根据下来列表来动态显示TabControl下的元素     需要准备两个控件:Combobox (命名为:cbPrjType)和 TabControl (命名为:tabPrjType),TabControl下面有六个子元素分别是:tabPage1,tabPage2,tabPage3,tabPag...
1037 0
|
JavaScript
easyui 隔行渐变色属性设置
1.部分JS文件 function getAgencyDealTable(begin,end,regionFlag,agencyId){ $('#reportList').datagrid({ idField : 'id', // 只要创建数据表格 就必须要加 ifFiel...
1107 0