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. CMAKE的用法

    一.      基本使用 安装:下载二进制包后可直接解压使用 从源码安装则执行命令:./bootstrap; make; make install——尝试执行bootstrap失败 使用:cmake ...

  2. yii表单

    yii  的dropdownlist,用yii的session可以记下选中的状态 $form = $this->beginWidget('CActiveForm',array('action'= ...

  3. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  4. 【转】java环境配置

    JAVA开发环境的搭建(配置JAVA开发环境) 一.安装JDK 1. JVM(Java Virtual Machine—Java虚拟机) JRE(Java Runtime Environment—Ja ...

  5. 在centos使用rpm包的方式安装mysql,以及更改root密码

    在centos使用rpm包的方式安装mysql,对于centos官方实际推荐使用yum进行安装,下载安装的方式主要用于内网服务器不能连接外网yum源的情况. 下载包 首先根据centos版本在mysq ...

  6. leetcode 38 Count and Say ---java

    这道题主要就是求一个序列,题目得意思就是 1 --> 11 --> 21 --> 1211 -->   111221 --> 312211 --> ..... 1个 ...

  7. linux之使用cron,logrotate管理日志文件

    1) logrotate配置   logrotate 程序是一个日志文件管理工具.用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”.   我们可以根据日志文件的大小,也可以根据其天数来 ...

  8. 作业:用HTML制作简历

    代码为: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  9. Firefox-常用扩展

    抓包: HttpFox,相比 Firebug 在页面跳转或刷新时依旧保持原有数据 常用User-Agent模拟: User Agent Switcher 更改请求头: ModifyHeaders 更改 ...

  10. Python-属性(property)

    在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]]) fget:获取属性 fset ...