自适应高度的表格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实现控件根据内容进行宽度自适应和高度自适应》。
原文地址:https://blog.csdn.net/jia12216/article/details/125856167
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_11703.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!