自定义 cell 自适应高度
#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 自适应高度的更多相关文章
- 自定义cell自适应高度
UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...
- 自定义cell 自适应高度
#pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...
- TableView cell自适应高度-----xib
1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExte ...
- 【swift,oc】ios开发中巧用自动布局设置自定义cell的高度
ios开发中,遇到自定义高度不定的cell的时候,我们通常的做法是抽取一个frame类,在frame类中预算好高度,再返回. 但是苹果出来自动布局之后...春天来了!!来看看怎么巧用自动布局设置自定义 ...
- Cell自适应高度及自定义cell混合使…
第一部分:UItableViewCellAdaptionForHeight : cell的自适应高度 第二部分:CustomTableViewCell:自定义cell的混合使用(以简单通讯录为例) = ...
- cell自适应高度
MyModel.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MyModel : ...
- IOS XIB Cell自适应高度实现
1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [ ...
- 自定义cell的高度
// // RootTableViewController.m // Share // // Created by lanouhn on 15/1/20. // Copyright (c) 2 ...
- iOS之UITableView加载网络图片cell自适应高度
#pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...
随机推荐
- NSNumber与NSInteger的区别 -bei
基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值. 类的初始化,需用类自身的方法 (类方法). 代码中所创建的对 ...
- Oracle 11gR2 RAC Votedisk and OCR Diskgroup Recovery
check votedisk and OCR [root@vzwc1 ~]# ocrcheck Status of Oracle Cluster Registry is as follows : Ve ...
- logstash 各种时间转换
<pre name="code" class="html">日期格式转换: /***** nginx 访问日志 [elk@zjtest7-front ...
- Android实现摇晃手机的监听
摘自:http://blog.csdn.net/xwren362922604/article/details/8515343 监听摇晃手机的类: /** * @author renxinwei ...
- 写在学习linux内核协议栈之前
一直很喜欢内核,但是新手,非常的痛苦啊.现在看一本linux内核协议栈源码解析一书,将自己学习的经历以及 理解记录下来,以备将来回头查漏补缺,同时校正自己的理解错误,自勉
- cocos2dx-lua绑定自定义c++类(一)
本文主要介绍mac上,如何将自定义的c++类,绑定到lua. 1.工具先行 找到 你的cocos2d-x/tools/tolua++,里面文件按类型大致分为: (1)*.pkg:用于定义要绑定的c++ ...
- 【hihoCoder第十五周】最近公共祖先·二
老实说我没有读题,看见标题直接就写了,毕竟hiho上面都是裸的算法演练. 大概看了下输入输出,套着bin神的模板,做了个正反map映射,但是怎么都得不了满分.等这周结束后,找高人询问下trick. 若 ...
- JPA字段映射(uuid,日期,枚举,@Lob)
转:http://www.cnblogs.com/tazi/archive/2012/01/04/2311588.html 主键: JPA主键的生成策略不像Hibernate那么丰富. @Id @Ge ...
- 在ubuntu14.04上部署hadoop2.6.3
一.在Ubuntu下创建hadoop组和hadoop用户 增加hadoop用户组,同时在该组里增加hadoop用户,后续在涉及到hadoop操作时,我们使用该用户. 1.创建hadoop用户组 2.创 ...
- Spring redirect直接返回项目根文件夹
return "redirect:/";