UITabelView的Cell自适应高度

http://www.jianshu.com/p/83e72f90d7c1

思路:因为cell高度不固定,需要动态赋值,所以用了最常见的cell内有一个model,model内有cellHeight的方式,在cell的model的set方法中给model的cellHeght赋值。

cell.m  (重写model属性的set方法)

-(void)setBaikeModel:(Model *)baikeModel{

self.titleLabel.text = baikeModel.title;

self.titleLabelHeightConstraint.constant = baikeModel.cellHeight;

}

model.m  (重写title属性的set方法)

-(void)setTitle:(NSString *)title{

_title = title;

//根据后台数据,动态计算cell高度

CGFloat height = [title heightWithFont: [JYEToolModel jyeFontWithName:@"PingFangSC-Regular" size:16] withinWidth:ScreenWidth - 30];

_cellHeight = height;

}

VC.m

需要注意cell高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

Model *baikeModel = [self.contentArray objectAtIndex:indexPath.row];

return baikeModel.cellHeight + 12+11+20;

}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 70;

}

补充小工具:

#pragma mark -根据宽度,字号来计算字符串的高度

- (float) heightWithFont: (UIFont *) font withinWidth: (float) width{

CGRect textRect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)

options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading

attributes:@{NSFontAttributeName:font}

context:nil];

return ceil(textRect.size.height);

}

#pragma mark -根据字号来计算字符串的宽度

- (float) widthWithFont: (UIFont *) font{

CGRect textRect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, font.pointSize)                 options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading

attributes:@{NSFontAttributeName:font}

context:nil];

return ceil(textRect.size.width);

}

#pragma mark -根据字符串的实际高度、行间距来计算新的字符串的高度

-(float) realHeightWithDefaultHeight:(float)defaultHeight withRowSpacing:(float)rowSpacing withFont:(UIFont *)font{

CGFloat oneRowHeight = [@"test" sizeWithAttributes:@{NSFontAttributeName:font}].height;

CGFloat rows = defaultHeight/oneRowHeight;

CGFloat realHeight = defaultHeight +(rows - 1)* rowSpacing;

return realHeight;

}

#import "JYEToolModel.h"

@implementation JYEToolModel

/*

*  自定义字号大小

*/

+(UIFont *)jyeFontWithName:(NSString *)name size:(CGFloat)size{

UIFont * font = nil;

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9){

font = [UIFont fontWithName:name size:size];

}else{

if([name rangeOfString:@"Medium"].location == NSNotFound){

font = [UIFont systemFontOfSize:size];

}else{

font = [UIFont boldSystemFontOfSize:size];

}

}

return font;

}

@end

时间: 2024-07-31 14:26:59

UITabelView的Cell自适应高度的相关文章

TableView cell自适应高度-----xib

1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExtendedLayout = UIRectEdgeNone;//将原点移动到navigationBar下面去了 tableView.estimatedRowHeight = 37.0;//估计cell的高度 tableView.rowHeight = UITableViewAutomaticDimens

第?一讲:UITableView 高级 自定义cell , cell自适应高度

一.自定义cell(包括cell的自定义,以及直接赋值的方法) 自定义cell就是创建一个UITableViewCell的子类. 把cell上的控件创建都封装在子类中,简化UIViewController中的代码 示例代码分析:(这个例子包括cell的自定义,以及直接赋值的方法) 1.需要建立tabelViewCell类, 在其中进行cell上控件的添加 2.在tabelViewCell.m中进行初始化,和layoutSubviews的frame布局的操作 tabelViewCell.h定义属性

IOS 控件 - UITableView 中的cell 自适应高度

当 UITableView 中有一个 label 的内容比较长的时候,就需要 cell 自适应高度来多行展示label: 首先设置 label 的 line 为0: 代码如下: // 为每一个 cell 预设置一个高度,可以提高效率 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } // 这里每一个 cell

自定义 cell 自适应高度

#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation CommodityCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:re

IOS XIB Cell自适应高度实现

1.代码实现Cell高度自适应的方法 通过代码来实现,需要计算每个控件的高度,之后获取一个cell的 总高度,比较常见的是通过lable的文本计算需要的高度. CGSize labelsize = [@"asdassdas" sizeWithFont:font constrainedToSize:CGSizeMake(320,2000) lineBreakMode:NSLineBreakModeWordWrap]; 这样就可以计算展示需要的高度,cell里面展示的时候可以在代理的方法内

自定义cell 自适应高度

#pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { //size:   文字最大范围 //options:计算高度 参数 //  NSStringDrawingUsesLineFragmentOrigin:指定 原点 绘制字符串片段起源和基线. //attributes:文字某个属性 通常是大小 //ios7 获取文本高 方法 CGRect rect

进击的UI-------------多种Cell混合使用&懒加载&自适应高度

1.自定义控件 2.model 3.多种cell混合使用 4自适应高度 5.懒加载

怎样让自定义Cell的图片和文本自适应高度

Let's do it! 首先创建一个Model类 包括一个图片名称属性 还有文字内容属性 #import <Foundation/Foundation.h> @interface Model : NSObject @property (nonatomic, copy) NSString *imageName; @property (nonatomic, copy) NSString *info; @end 创建一个继承于UITableViewCell的MyTableViewCell,把模型作

[Swift通天遁地]二、表格表单-(3)在表格中嵌套另一个表格并使Cell的高度自适应

本文将演示如何在表格中嵌套另一个表格并使Cell的高度自适应,创建更加强大的布局效果. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Touch Class]->[Next]-> [Class]:CustomizeUITableViewCell ,类名. [Subclass of]:UITableViewCell ,父类 [Language]:Swift ->[Next]->[Create]在项目导航区,打开刚刚创建的代码