#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. Caffe : Layer Catalogue(2)

    TanH / Hyperbolic Tangent 类型(type):TanH CPU 实现: ./src/caffe/layers/tanh_layer.cpp CUDA.GPU实现: ./src/ ...

  2. cf C. Knight Tournament

    http://codeforces.com/contest/357/problem/C #include <cstdio> #include <cstring> #includ ...

  3. pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序

    pywin32 安装错误 ImportError: DLL load failed:  不是有效的 Win32 应用程序. 发现是因为没有制定Pywin32的dll所致,我们在用Pywin32开发时, ...

  4. BZOJ1532: [POI2005]Kos-Dicing

    1532: [POI2005]Kos-Dicing Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1060  Solved: 321[Submit][St ...

  5. Android Studio:You need to use a Theme.AppCompat theme (or descendant) with this activity. AlertDialog

    学习<第一行代码>的时候遇到的问题. Process: com.example.sevenun.littledemo, PID: 2085 java.lang.RuntimeExcepti ...

  6. 56个PHP开发常用代码

    2016/02/14 6203 4    在编写代码的时候有个神奇的工具总是好的!下面这里收集了 50+ PHP 代码片段,可以帮助你开发 PHP 项目. 这些 PHP 片段对于 PHP 初学者也非常 ...

  7. 获取某个文件夹中所有txt文件

    <?php // 获取文件夹中的所有txt文件名 $dir = "D:/a"; //这里输入其他路径 $handle = opendir($dir."." ...

  8. 如何将XML转换成XSD(XML Schema)文件

    将xml装换为xsd,先决条件是已经安装了Visual Stutio 1) 输入cmd在运行窗口 2) 将xsd的路径加入到path变量 set path=%path%;C:\Program File ...

  9. sublime怎么实现函数之间的跳转

    1.安装ctags应用程序. 到CTags的官方站点下载最新版本号,将解压后的ctags.exe放到系统环境变量的搜索路径中.通常是C:\windows\system32. 假设你想放到其它目录中,记 ...

  10. Win32多线程编程(1) — 基础概念篇

      内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...