自定义 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 ...
随机推荐
- delegate,notifucation,KVO三种模式实现通信的优缺点
在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delega ...
- Objective-C 静态变量 使用方法
详解Objective-C中静态变量使用方法 Objective-C中静态变量使用方法是本文要介绍的内容,Objective-C 支持全局变量,主要有两种实现方式:第一种和C/C++中的一样,使用&q ...
- 【转】Android 之 下拉框(Spinner)的使用
原文网址:http://imshare.iteye.com/blog/770950 下拉列表 Spinner. Spinner的使用,可以极大提高用户的体验性.当需要用户选择的时候,可以提供一个下拉列 ...
- 【转】Win7与Ubuntu 14.04双系统修改启动项顺序
原文网址:http://blog.sina.com.cn/s/blog_b381a98e0102v1gy.html 一.Ubuntu14.04采用默认安装方式的情况 采用默认安装方式,在win7下安装 ...
- Android Spinner使用简介
Android中使用Spinner作为下拉列表,下面直接看实现方式: (1)使用ArrayAdapter来实现: 实现步骤: 1. 在布局文件中定义Spinner组件: 2. 向Spinner添加需要 ...
- ubuntu系统修改终端提示符及设置颜色高亮
Linux终端大家想必都清楚吧,最近在使用的时候发现在进入到某个文件夹目录比较深的层次后,终端提示的绝对路径很长,这样给人的感觉很不习惯,在这里给大家介绍下如何修改终端的提示,顺便介绍下提示符的颜色: ...
- JAVA联调接口跨域解决办法
JAVA联调接口跨域解决办法 第一种代码: HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,HttpStatus. ...
- python socket学习
import socket localip=socket.gethostbyname(socket.gethostname()) print (localip) iplist=socket.getho ...
- ASIHttpRequest:创建队列、下载请求、断点续传、解压缩
ps:本文转载自网络:http://ryan.easymorse.com/?p=12 感谢作者 工程完整代码下载地址:RequestTestDownload1 可完成: 下载指定链接的zip压缩文件 ...
- CSS结构伪类E:first-child/last-child/only-child/empty
E:first-child解释:E的父元素的第一个子元素正好是E,给这个E定义样式 E:last-child解释:E的父元素的最后一个子元素正好是E,给这个E定义样式 E:only-child解释:E ...