自适应高度的表格UICollectionView

32 篇文章 0 订阅
18 篇文章 0 订阅

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值