zoukankan      html  css  js  c++  java
  • ios . -- UICollectionView --cell 自适应

    #pragma mark — 视图控制器中使用:(关键)
        layout.estimatedItemSize = CGSizeMake(WIDTH, 60);  // layout约束这边必须要用estimatedItemSize才能实现自适应,使用itemSzie无效
    //
    // 商品详情 容器 详情 cell
    
    #import <UIKit/UIKit.h>
    
    @interface DetailsViewCell : UICollectionViewCell
    @property (nonatomic,strong) CategorizeListOfGoodsModel *goodsitemmodel;
    
    @end
    #import "DetailsViewCell.h"
    
    @interface DetailsViewCell()
    
    @property (nonatomic,strong) UILabel *titletxt;//标题
    @property (nonatomic,strong) UILabel *pracetxt;//价格
    @property (nonatomic,strong) UILabel *msaletxt;//销量
    
    @end
    
    @implementation DetailsViewCell
    
    
    - (void)setGoodsitemmodel:(CategorizeListOfGoodsModel *)goodsitemmodel
    {
        _goodsitemmodel = goodsitemmodel;
        self.titletxt.text = [NSString stringWithFormat:@"%@",goodsitemmodel.title];
        self.pracetxt.text = [NSString stringWithFormat:@"¥%@",goodsitemmodel.voucher_price];
        self.msaletxt.text = [NSString stringWithFormat:@"月销%@",goodsitemmodel.m_sale];
        
        
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(0);
            make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
            make.bottom.mas_equalTo(self.pracetxt.mas_bottom).offset(10);
        }];
        
        
        [self.titletxt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).mas_offset(10);
            make.left.equalTo(self).mas_offset(10);
            make.right.equalTo(self).mas_offset(-10);
        }];
        
        [self.pracetxt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titletxt.mas_bottom).mas_offset(10);
            make.bottom.equalTo(self).mas_offset(-10);
            make.left.equalTo(self).mas_offset(10);
        }];
    
        [self.msaletxt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titletxt.mas_bottom).mas_offset(10);
            make.bottom.equalTo(self).mas_offset(-10);
            make.right.equalTo(self).mas_offset(-10);
        }];
    
    }
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self initWithUIFrame:frame];
        }
        return self;
    }
    
    - (void)initWithUIFrame:(CGRect)rect
    {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.titletxt = [[UILabel alloc]init];
        self.titletxt.font = [UIFont systemFontOfSize:14];
        self.titletxt.textColor = [YColor YColorWithHexString:@"#333333"];
        self.titletxt.numberOfLines = 0;
        [self.contentView addSubview:self.titletxt];
        
        self.pracetxt = [[UILabel alloc]init];
        self.pracetxt.font = [UIFont systemFontOfSize:16];
        self.pracetxt.textColor = [YColor YColorWithHexString:@"#F32F19"];
        [self.contentView addSubview:self.pracetxt];
        
        self.msaletxt = [[UILabel alloc]init];
        self.msaletxt.font = [UIFont systemFontOfSize:12];
        self.msaletxt.textColor = [YColor YColorWithHexString:@"#999999"];
        [self.contentView addSubview:self.msaletxt];
        
        
    }
    
    #pragma mark — 实现自适应文字宽度的关键步骤:item的layoutAttributes
    - (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
  • 相关阅读:
    leetCode21. 合并两个有序链表
    (flag)每日三道面试题(4.25)
    每日三加一面试题(4.21)
    按照顺序执行异步函数
    javascript Proxy 代理模式深度监听对象、数组变化
    数据结构与算法(二) 线性表一
    This关键字
    数据结构与算法(一)
    Git报错信息
    MakeDown语法学习
  • 原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/9322669.html
Copyright © 2011-2022 走看看