#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. 在类库中使用log4net

    最近在做一个类库,用的C#写的,为了DEBUG方便需要日志输出,于是找了log4net这个工具进行日志输出; 因为调用这个类库的是C++,而且本人对C++不是很熟悉,于是无法在app.config或者 ...

  2. 使用jcifs.smb.SmbFile读取Windows上共享目录的文件

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Servl ...

  3. Android Tab控件简介

    在Android中,Tab控件是一种很常用的控件:Tab控件即标签页,可以在一页中切换显示N页内容: Tab控件具有两种实现过程,一是在同一个Activity中切换显示不同的标签页,这种主要是通过修改 ...

  4. 处理通过<input type="file">的Post 请求

    [HttpPost] public ActionResult Cal() { string ExcelName = System.DateTime.Now.ToString("yyyyMMd ...

  5. c语言结构体1之定义

    这是在复习阶段随便小结的一些东西 别喷哦 结构体定义的三种方式 注意事项: 1结构体括号后面有分号 2#define得放在程序上面 3成员名可以和结构体名相同 4结构体类型不能直接访问成员,也不能赋值 ...

  6. Java常见内存溢出异常分析(OutOfMemoryError)

    原文转载自:http://my.oschina.net/sunchp/blog/369412 1.背景知识 1).JVM体系结构 2).JVM运行时数据区 JVM内存结构的相关可以参考: http:/ ...

  7. [Javascript] Web APIs: Persisting browser data with window.localStorage

    Local Storage is a native JavaScript Web API that makes it easy to store and persist data (as key-va ...

  8. getInitParameter()

      getInitParameter()方法是在GenericServlet接口中新定义的一个方法,用来调用初始化在web.xml中存放的参量.在web.xml配置文件中一个servlet中参量的初始 ...

  9. single-row function和muti-row function

    1.single-row function 指一行数据输入,返回一个值的函数. 常见的有 字符函数(如:substr) 日期函数(如:months_between) 数字函数(如:MOD) 转换函数( ...

  10. SSH 服务启动时出现如下错误:fatal: Cannot bind any address

    注意:本文相关配置及说明已在 CentOS 6.5 64 位操作系统中进行过测试.其它类型及版本操作系统配置可能有所差异,具体情况请参阅相应操作系统官方文档. 问题描述 云服务器 ECS (Elast ...