自定义 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 ...
随机推荐
- gtest以及测试小结
所有的测试,都是让未知的东西和已知的东西进行比较,如果测试结果和预期的一样,那么就认为被测对象是OK的否则视为有问题. python的单元测试是写一堆继承了unittest.TestCase类,每个类 ...
- Gradle Android客户端程序打包(基于gradle 1.12版本验证通过)
一.前言 android客户端开发进入尾声,负责SEO同事突然发给我一个涉及45个发布渠道的噩耗,之前只发布自有渠道的工作方式(手动修改参数打包)已经不满足需求,所以引入最近比较流行的gradle打包 ...
- eucMenu
- C++程序面试笔迹六
1.const和static的作用 用最熟悉的语言: 2.阶乘求和 如:1!+2!+3!+4!+……+n!=sum? 3.删除掉字符串中相同的字母只留有一个 如: akkkjjtyy——> ...
- c语言函数定义、函数声明、函数调用以及extern跨文件的变量引用
1.如果没有定义,只有声明和调用:编译时会报连接错误.undefined reference to `func_in_a'2.如果没有声明,只有定义和调用:编译时一般会报警告,极少数情况下不会报警告. ...
- Eclipse+Java+OpenCV246环境搭建和代码测试
1.首先下载OpenCV2.4.6,下载的时候,选择windows版的.然后安装 2.其实安装的过程就是解压的过程,并没有什么安装向导之类的,安装完成后,我们最关心的是这个目录:opencv\buil ...
- Linux内核设计基础(十)之内核开发与总结
(1)Linux层次结构: (2)Linux内核组成: 主要由进程调度(SCHED).内存管理(MM).虚拟文件系统(VFS).网络接口(NET)和进程间通信(IPC)等5个子系统组成. (3)与Un ...
- 分享几个社交类网站常用并且也是最好用的jquery类库
官网都有详细的文档说明,大家自行百度谷歌哈! artZoom:常用于微博,支持图片放大缩小旋转 AutoComplete:自动完成 BackTop:当内容多时出现“返回顶部” CFUpload:批量上 ...
- hdu1047(模拟大量的循环添加)
标题信息:总结多个大整数,(使用add循环相加的功能) http://acm.hdu.edu.cn/showproblem.php? pid=1047 AC代码: /** *大数的循环加法,转化为字 ...
- UVA 10198 Counting
Counting The Problem Gustavo knows how to count, but he is now learning how write numbers. As he is ...