#pragma mark — 视图控制器中使用:(关键)
layout.estimatedItemSize = CGSizeMake(WIDTH, ); // 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();
make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);
make.bottom.mas_equalTo(self.pracetxt.mas_bottom).offset();
}]; [self.titletxt mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).mas_offset();
make.left.equalTo(self).mas_offset();
make.right.equalTo(self).mas_offset(-);
}]; [self.pracetxt mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titletxt.mas_bottom).mas_offset();
make.bottom.equalTo(self).mas_offset(-);
make.left.equalTo(self).mas_offset();
}]; [self.msaletxt mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titletxt.mas_bottom).mas_offset();
make.bottom.equalTo(self).mas_offset(-);
make.right.equalTo(self).mas_offset(-);
}]; } - (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:];
self.titletxt.textColor = [YColor YColorWithHexString:@"#333333"];
self.titletxt.numberOfLines = ;
[self.contentView addSubview:self.titletxt]; self.pracetxt = [[UILabel alloc]init];
self.pracetxt.font = [UIFont systemFontOfSize:];
self.pracetxt.textColor = [YColor YColorWithHexString:@"#F32F19"];
[self.contentView addSubview:self.pracetxt]; self.msaletxt = [[UILabel alloc]init];
self.msaletxt.font = [UIFont systemFontOfSize:];
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

ios . -- UICollectionView --cell 自适应的更多相关文章

  1. IOS XIB Cell自适应高度实现

    1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [ ...

  2. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

  3. TableView cell自适应高度-----xib

    1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...

  4. iOS 在cell中使用倒计时的处理方法(新)

    一.前言 之前的文章iOS 在cell中使用倒计时的处理方法得到大量的支持, 在这先感谢大家的支持. 但是也收到不少人的回复表示不会用, 需要一一解答, 由于之前写的时候没有使用Markdown编辑, ...

  5. iOS tableview cell 的展开收缩

    iOS tableview cell 的展开收缩 #import "ViewController.h" @interface ViewController ()<UITabl ...

  6. iOS UICollectionView 长按移动cell

    ref:http://www.jianshu.com/p/31d07bf32d62 iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图 ...

  7. iOS UICollectionView高级用法(长按自由移动cell)-新

    [reference]http://www.jianshu.com/p/31d07bf32d62 iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. ...

  8. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  9. iOS UICollectionView高级用法(长按自由移动cell)

    iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好. @property (nonatomic, strong) UIColle ...

随机推荐

  1. [GitHub开源]基于HTML5实现的轻量级Google Earth三维地图引擎,带你畅游世界 【转】

    http://blog.csdn.net/iispring/article/details/52679185 WebGlobe HTML5基于原生WebGL实现的轻量级Google Earth三维地图 ...

  2. char p[]与char *p的区别

    #include <iostream> using namespace std; int main(void) { char *pp = "abc";//*pp指向的是 ...

  3. Netty源码细节IO线程(EventLoop)(转)

    原文:http://budairenqin.iteye.com/blog/2215896 源码来自Netty5.x版本, 本系列文章不打算从架构的角度去讨论netty, 只想从源码细节展开, 又不想通 ...

  4. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  5. Web应用——驾培管理系统之个人管理(作者:小圣)

    Web应用--驾培管理系统之个人管理(作者:小圣) 本节博文旨在实现本次Web应用的个人管理功能.能够在登陆后 查看并改动个人信息或者进行password改动.同一时候,在输入的时候进行表单验证,验证 ...

  6. Win7如何改变用户文件夹位置

    现在装WIN7后第一件就是改变用户账户文件夹位置..因为里面存着一些软件的设定和信息等..不必要每次装后都手动改一次.. 已前用优化大师改.太麻烦.也不稳定有时有些目录不能完全改过来.. 通过命令mk ...

  7. react-native + teaset(Drawer)实现侧边菜单

    1.代码 /** * 购物车 */ import React, {Component} from 'react'; import { View, Image, } from 'react-native ...

  8. Visual studio C++ 之空控制台工程添加文件并解决头文件包含问题

    背景 之前都是用的MFC已搭好框架,今天想自己从零开始完成添加文件,编译,并解决文件包含问题,在此做个记录. 正文 首先创建一个Win32控制台应用程序,不要直接点击完成,而是点击下一步,然后选择空项 ...

  9. iOS 两种不同的图片无限轮播

    代码地址如下:http://www.demodashi.com/demo/11608.html 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵, ...

  10. 了解机器学习框架CoreML

    代码地址如下:http://www.demodashi.com/demo/11972.html CoreML是iOS 11新推出的机器学习框架,是人工智能的核心内容,他可以在训练好的机器学习模型应用到 ...