#import "CommodityCell.h"

#import "UIImageView+WebCache.h"

@implementation CommodityCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

// Initialization code

[self addAllViews];

}

return self;

}

#pragma mark 加载全部控件

- (void)addAllViews

{

// 图片

self.photoImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(kMargin, kMargin, kWidth, kWidth)] autorelease];

_photoImageView.backgroundColor = [UIColor clearColor];

[self.contentView addSubview:_photoImageView];

// 标题

self.titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_photoImageView.frame) + kMargin, kMargin, 225, kWidth / 2 - 10)] autorelease];

_titleLabel.backgroundColor = [UIColor clearColor];

_titleLabel.font = [UIFont boldSystemFontOfSize:17];

[self.contentView addSubview:_titleLabel];

_titleLabel.numberOfLines = 0;

// 详情

self.introduceLabel = [[[UILabel alloc] initWithFrame:CGRectMake(140, CGRectGetMaxY(_titleLabel.frame) + kMargin, 225, kWidth / 2)] autorelease];

_introduceLabel.backgroundColor = [UIColor clearColor];

_introduceLabel.numberOfLines = 0;

[self.contentView addSubview:_introduceLabel];

// 关闭交互

self.contentView.userInteractionEnabled = NO;

}

#pragma mark - 计算模型内某个字符串的高度

+ (CGFloat)calsLabelHeightWithCommodity:(Commodity *)commodity

{

// size: 表示允许文字所在的最大范围

// options: 一个参数,计算高度是使用  NSStringDrawingUsesLineFragmentOrigin

// attribute: 表示文字的某个属性(通常是文字大小)

// context: 上下文对象,通常写nil

CGRect rect = [commodity.Descripition boundingRectWithSize:CGSizeMake(225, 500) options:NSStringDrawingUsesLineFragmentOrigin

attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]}

context:nil];

return rect.size.height;

}

#pragma mark 使用模型方法,返回模型内容在自己内部显示应该的高度

+ (CGFloat)cellHeightWithCommodity:(Commodity *)commodity

{

CGFloat a = 50 + 3 * kMargin + [self calsLabelHeightWithCommodity:commodity];

CGFloat b = kMargin + kWidth; // 图片的高度

if (a < b) {

return b;

} else {

return a;

}

}

#pragma mark 重写 commodity的setter方法

- (void)setCommodity:(Commodity *)commodity

{

NSLog(@"%@", commodity.Descripition);

if (_commodity != commodity) {

[_commodity release];

_commodity = [commodity retain];

}

//1.标题

self.titleLabel.text = _commodity.title;

//2.1 自适应高度

CGRect frame = _introduceLabel.frame;

frame.size.height = [CommodityCell calsLabelHeightWithCommodity:_commodity];//调整高度

_introduceLabel.frame = frame;

//2.2显示文字

self.introduceLabel.text = _commodity.Descripition;

//3. SDWebImage 异步加载图片

[self.photoImageView sd_setImageWithURL:[NSURL URLWithString:_commodity.s_image_url]];

}

自定义 cell 自适应高度的更多相关文章

  1. 自定义cell自适应高度

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

  2. 自定义cell 自适应高度

    #pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...

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

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

  4. 【swift,oc】ios开发中巧用自动布局设置自定义cell的高度

    ios开发中,遇到自定义高度不定的cell的时候,我们通常的做法是抽取一个frame类,在frame类中预算好高度,再返回. 但是苹果出来自动布局之后...春天来了!!来看看怎么巧用自动布局设置自定义 ...

  5. Cell自适应高度及自定义cell混合使…

    第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...

  6. cell自适应高度

    MyModel.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MyModel : ...

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

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

  8. 自定义cell的高度

    // //  RootTableViewController.m //  Share // //  Created by lanouhn on 15/1/20. //  Copyright (c) 2 ...

  9. iOS之UITableView加载网络图片cell自适应高度

    #pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...

随机推荐

  1. Touch事件

    http://www.cnblogs.com/shawn-xie/archive/2012/12/07/2805582.html 前言 一个触屏网站到底和传统的pc端网站有什么区别呢,交互方式的改变首 ...

  2. LeetCode_Climbing Stairs

    ou are climbing a stair case. It takes n steps to reach to the top. or steps. In how many distinct w ...

  3. 通过代理访问nginx和直接访问nginx区别

    80.82.78.38 [23/Sep/2016:05:36:18 +0800] "GET http://www.baidu.com/cache/global/img/gs.gif HTTP ...

  4. poj:2992 因子数量

    题意: 求 组合数c(n,k)的因子数量 由算术基本定理很容易求得,不过第一次却T了,加了好多预处理,o1查询,才过 #include <iostream> #include <st ...

  5. You don't have permission to access / on this server for debian_8

    Forbidden You don't have permission to access / on this server. Apache/2.4.10 (Debian) Server at www ...

  6. 在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦

    在线服装零售商Betabrand获得650万美元风投 - 投资风向 - 创业邦 在线服装零售商Betabrand获得650万美元风投

  7. hdu 5400 Arithmetic Sequence(模拟)

    Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence ≤i≤n) such that ≤j& ...

  8. Mongo服务器集群配置【转】

    http://www.cnblogs.com/wly923/tag/MongoDB/ 当前标签: MongoDB   Mongo服务器集群配置学习三——分片 风行影者 2013-04-14 22:35 ...

  9. NVL函数(NVL,NVL2,NULLIF,COALESCE)

    NVL 语法:NVL( expr1, expr2) 功能:如果expr1为NULL,则NVL函数返回expr2的值,否则返回expr1的值,如果两个参数的都为NULL ,则返回NULL. 注意事项:e ...

  10. oracle的启动和关闭

    一.sql*plus方式:      用sql*plus来连接到Oracle Sqlplus /nolog 是以不连接数据库的方式启动sql*plus Connect /as sysdba 是以DBA ...