tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方。现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell
首先要准备两个模型,一个是存放数据的模型,另一个计算cell高度的模型。

具体代码分析如下:(红色为关键代码)
 在ViewController.m中
//懒加载
-(NSArray *)allmodel{ if (_allmodel==nil) { NSString *path = [[NSBundle mainBundle]pathForResource:@"model.plist" ofType:nil]; NSArray *arry = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *arry = [NSMutableArray array]; for (NSDictionary *dic in arry) { Model *model = [Modelstaue modelWithstaue:dic];
26
27 ModelFrame *modelframe = [[ModelFrame alloc]init];
28
29 modelframe.Fmodel = model;
30
31 [allist addObject:modelframe];
32
} _allmodel = arry; } return _allmodel; } 其他的代码,暂时不管 开始计算。。。 在计算cell高度的模型中ModelF中 ModelFra.h中 @class Modelstaue; @interface ModelFrame : NSObject //CGRect 后面可不要不小心入了*这个哦 头像的frame @property (nonatomic, assign, readonly) CGRect touF; 昵称的frame @property (nonatomic, assign, readonly) CGRect nameF; 会员图标的frame @property (nonatomic, assign, readonly) CGRect vipF; 正文的frame @property (nonatomic, assign, readonly) CGRect textF; 图片的frame @property (nonatomic, assign, readonly) CGRect pictureF; cell的高度 @property (nonatomic, assign, readonly) float cellHeight; @property(nonatomic,strong)Modelstaue *modelF; 在#import "modelFra.h",也就是.m文件中 #define NameFont [UIFont systemFontOfSize:14] // 正文的字体 #define TextFont [UIFont systemFontOfSize:15] @implementation ModelFra -(void)setModelF:(Modelstaue *)modelF{ _modelF = modelF; // 1.头像 _touF = CGRectMake(,,,); // 2.昵称 CGSize nameSize = [self sizeWithText:self.status.name font:MJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];//先计算出宽高,后面方便算出坐标 MAXFLOAT-最大值 CGFloat nameX = CGRectGetMaxX(_touF) + ; CGFloat nameY = 10 + (30 - nameSize.height) /2;//让昵称保持在右边中间 _nameF = CGRectMake(nameX, nameY, nameSize.width, nameSize.height); // 3.会员图标 _vipF = CGRectMake(CGRectGetMaxX(_nameF) + , nameY, , ); // 4.正文 CGFloat textX = iconX; CGFloat textY = CGRectGetMaxY(_iconF) + padding; CGSize textSize = [self sizeWithText:self.status.text font:TextFont maxSize:CGSizeMake(300, MAXFLOAT)];//这里就不用在写成了MAXFLOAT,正文会超出屏幕的宽 _textF = CGRectMake(, CGRectGetMaxY(_touF) + , textSize.width, textSize.height); // 5.图 if (self.modelF.picture) { // 图 _pictureF = CGRectMake(,CGRectGetMaxY(_textF) + , , ); //根据所以的子控件的高度,从而得到了cell的高度 _cellHeight = CGRectGetMaxY(_pictureF) + ; } else { _cellHeight = CGRectGetMaxY(_textF) + ;//无图的话 } } //根据文字的大小和尺寸来确定宽高的公式(封装了计算方法) - (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
170
171 {
172
173 NSDictionary *attrs = @{NSFontAttributeName : font};
174
175 return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
176
177 }
 #import "WTableViewCell.h"//在cell的.m文件里
#import "ModelFra.h"
#import "Model.h"
@interface WTableViewCell()
@property (nonatomic, retain) UIImageView *touView;
/**
* 昵称
*/
@property (nonatomic, retain) UILabel *nameView;
/**
* 会员
*/
@property (nonatomic, retain) UIImageView *vipView;
/**
* 正文
*/
@property (nonatomic, retain) UILabel *textView;
/**
* 图
*/
@property (nonatomic, retain) UIImageView *pictureView;
@end @implementation WTableViewCell -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 头像
self.touView = [[UIImageView alloc]init]; [self.contentView addSubview:self.touView]; // 昵称
self.nameView = [[UILabel alloc]init];
self.nameView.font = [UIFont systemFontOfSize:14];
self.nameView.numberOfLines = ;
****千万不要忘记给定字体大小了,不要会出现比如导致出现了label上的文字和背景分离的奇怪现象,这是血的教训
[self.contentView addSubview:self.nameView];
// 图标
self.vipView = [[UIImageView alloc]init];
[self.contentView addSubview:self.vipView];
// 内容
self.textView = [[UILabel alloc]init];
self.textView.font = [UIFont systemFontOfSize:15];
、、不要以为计算模型那里已经赋值,两个概念,完全不一样,现在要给定大小装进计算好的模型里面去
self.textView.numberOfLines = ;
[self.contentView addSubview:self.textView];
// 配图
self.pictureView = [[UIImageView alloc]init];
[self.contentView addSubview:self.pictureView]; }
return self;
} -(void)setModelF:(ModelFrame *)modelF{
_modelF = modelF;
// 数据(当然也不是非得把数据放到这里,只是这样可以减少vc里面的代码)
[self setingData] ;
// 计算高度(为什么用到另外一个模型来计算呢?因为在自定义cell里面计算的话,cell的高度无法传出去)
[self setingFrame]; } //传入模型数据
-(void)setingData{
Model *model = self.modelframe.status;
self.touView.image = [UIImage imageNamed:model.tou];
self.textView.text = model.text; self.nameView.text = model.name;
下面的判断就可以做出有图片和有无会员图标的啦,要是把数据放在vc那里的话 到这里就相当麻烦了
if (model.picture) {//这里要作判断
81 self.pictureView.hidden = NO;//如果有图片 那就不要隐藏
82 self.pictureView.image = [UIImage imageNamed:model.picture];
83 }else{
84 self.pictureView.hidden = YES;//没有图片的话 就隐藏
85 } if (model.vip) {
88 self.vipView.hidden = 0;
89 self.nameView.textColor = [UIColor redColor];
90 }else{
91 self.vipView.hidden = 1;
92 self.nameView.textColor = [UIColor blackColor];
93 }
} //计算尺寸
-(void)setingFrame{
self.iconView.frame = self.modelframe.touF;
self.nameView.frame = self.modelframe.nameF;
self.textView.frame = self.modelframe.textF;
self.vipView.frame = self.modelframe.vipF; if (self.modelframe.status.picture) { self.pictureView.frame = self.modelF.pictureF; } self.pictureView.frame = self.modelF.pictureF; } //初始化放到这里来
+ (instancetype)cellWithTableView:(UITableView *)tableView
{ static NSString *ID = @"HHHHH";
WTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell==nil) {
cell = [[WTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
return cell;
} @end

补充哈( ^_^ )这里cell的.h文件

 #import <UIKit/UIKit.h>
@class ModelFrame;
@interface WTableViewCell : UITableViewCell
@property(nonatomic,retain)ModelFrame *modelframe; + (instancetype)cellWithTableView:(UITableView *)tableView;
@end

数据模型model.h

#import <Foundation/Foundation.h>

@interface Modelstaue : NSObject
@property(nonatomic,copy)NSString *text;
@property(nonatomic,copy)NSString *tou;
@property(nonatomic,copy)NSString *name;
@property (nonatomic, copy) NSString *picture;
@property(nonatomic,assign)NSString *vip; //将字典转化为模型
-(instancetype)initWithmodel:(NSDictionary *)dic;
初始cell的类方法
+(instancetype)modelWithstaue:(NSDictionary *)dic; @end

数据模型model.m

#import "Model.h"

@implementation Modelstaue

+(instancetype)modelWithstaue:(NSDictionary *)dic{
return [[self alloc]initWithstaue:dic];
}
-(instancetype)initWithstaue:(NSDictionary *)dic{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
} @end

终于来到了vc.m文件里面了,只展示部分代码,其他的都很简单了

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.allmodel.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//初始化和赋值的代码不放在这里,控制器好轻松。。。。仅仅三句代码
WTableViewCell *cell = [WTableViewCell cellWithTableView:tableView]; cell.modelframe = self.allmodel[indexPath.row]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ ModelFrame *mof = _allmodel[indexPath.row];
return mof.cellHeight; }

IOS开发-cell的动态高度的更多相关文章

  1. ios 实现 cell 的动态高度

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

  2. iOS开发——C篇&动态内存分配

    再C语言中关于内存是一个很重要的知识点,所以今天我就从c语言的内存分配开始为大家解析一下C语言再iOS开发中非常重要的一些知识. 1:malloc函数的介绍 C语言中开辟内存空间:malloc函数 再 ...

  3. iOS开发——C篇&动态内存分析

    再C语言中关于内存是一个很重要的知识点,所以今天我就从c语言的内存分配开始为大家解析一下C语言再iOS开发中非常重要的一些知识. 1:malloc函数的介绍 C语言中开辟内存空间:malloc函数(堆 ...

  4. iOS开发之计算动态cell的高度并缓存

    项目中有个类似微博那样的动态cell,文字和图片的多少都不是确定的 刚开始使用autolayout,结果很多问题,最后我发现了一个框架 FDTemplateLayoutCell 写的很好,自动布局ce ...

  5. iOS开发——Autolayout下动态调整单元格高度

    情景描述: 有时候我们希望更新某一个单元格的数据,通常的做法是使用reloadData方法更新整个单元格.但是对一些情况是不适用的或者说实现起来比较麻烦.比如说这种简单的"点开"一 ...

  6. [iOS开发]TextKit之动态改变样式

    在iOS中有一项功能,就是用户可以自定义设备的字体大小,粗体和其他一些样式.具体可以选择 “设置” ---- “通用” ---- “字体大小“ / “辅助功能”, 重新调整文本字体的样式. 如何使ap ...

  7. iOS开发之监听键盘高度的变化

    最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...

  8. iOS开发之监听键盘高度的变化 分类: ios技术 2015-04-21 12:04 233人阅读 评论(0) 收藏

    最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...

  9. iOS开发 准确计算Coretext高度

    - (int)getAttributedStringHeightWithString:(NSAttributedString *)  string  WidthValue:(int) width{   ...

随机推荐

  1. LeetCode Word Pattern (模拟)

    题意: 给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern. 思路: 注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同, ...

  2. CSS 实现:父元素包含子元素,子元素垂直居中布局

    ☊[实现要求]:父元素包含子元素,子元素垂直居中布局 <div class="demo5"> <div class="child">A& ...

  3. python with as用法

    with as用途:取代原来的try...finally,主要是用于处理异常 基本语法:with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本要求:with所求值的对 ...

  4. Cortana 在安装语言包后失灵 | 解决

    http://windows.microsoft.com/zh-cn/windows-10/cortanas-regions-and-languages 适用于 Windows 10 Currentl ...

  5. 作业6 NABCD模型分析,产品Backlog

    1.N(Need 需求): 随着生活水平的提高,每个家庭中都会有电脑和移动设备,可以更加快捷方便使用软件.以前孩子练习计算能力需要通做习题卷或老师出题目来进行,但现在只要通过这个四则运算的程序,可以自 ...

  6. puppet安装配置及使用

     puppet安装前准备 一.服务器信息 master端:10.10.10.201 master.fansik.com slave端:10.10.10.156 slave.fansik.com 三台机 ...

  7. GRANT ALL PRIVILEGES 限制某个或所有客户端都可以连接至mysql

    GRANT ALL PRIVILEGES 1. 改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mys ...

  8. 越狱Season 1-Episode 14: The Rat

    Season 1, Episode 14: The Rat -Michael: 24 hours from now, 24小时后 my brother is scheduled to die, sch ...

  9. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

  10. Scrum 项目2.0

    阅读教材第8章,8.1~8.3节 P157~168,了解获取用户需求的办法,每个组可以选择一二加以应用. 8.4节P168-171 查阅NABCDA模型的具体说明. 2.SCRUM 流程的步骤 1 完 ...