自定义 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 ...
随机推荐
- R语言教程规划
本文发表在博客园, http://www.cnblogs.com/stackworm/ 尽管进展中出现了意想不到的事情,期间中断1个多月,但我仍然会坚持下去. 首先,这份教程适合所有对R语言有兴趣且希 ...
- 官方recovery签名验证的破解教程
下面讲如何破解官方recovery签名验证(这个方法应该是通用的,其他手机可以参考,recovery签名验证破解了,也不用费力编译第三方recovery) 1.从官方ROM里提取recovery.im ...
- 使用jcifs.smb.SmbFile读取Windows上共享目录的文件
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Servl ...
- android开发3:四大基本组件的介绍与生命周期
android开发3:四大基本组件的介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver ...
- 超强Altium Designer焊盘为梅花状连接,过孔为直接连接的方法
AltiumDesigner6焊盘为梅花(或十字)状连接,过孔为直接连接的方法: 一.完成后效果 二.PCB规则设置(PCBRULES) 三.添加IsVia+ 四.添加InNamedPolygon() ...
- keil C语言与汇编语言混合编程
C与汇编混合编程主要有以下几种:(1)C语言中嵌入汇编(2)无参数传递的函数调用(3)有参数传递的函数调用 一.C语言中嵌入汇编 1.在 C 文件中要嵌入汇编代码片以如下方式加入汇编代码: #prag ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- python3-day3(内置函数)
1.内置函数 1>print(bytearray('王',encoding='utf8')) 2>print(bytes('王',encoding='utf8')) 3>bool(' ...
- Java 高效 MVC & REST 开发框架 JessMA v3.2.1 即将发布
JessMA(原名:Portal-Basic)是一套功能完备的高性能 Full-Stack Web 应用开发框架,内置可扩展的 MVC Web 基础架构和 DAO 数据库访问组件(内部已提供了 ...
- Direct3D 11的流水线
流水线 流水线(Pipeline)是理解D3D必须要掌握的概念. 整个流水线有很多步骤,有的步骤是固定功能,不用怎么配置,有的步骤是要写代码的,也就是所谓的着色器程序(Shader). 一般来说,将流 ...