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. socket() failed (13: Permission denied) while connecting to upstream

    /*************************************************************************** * socket() failed (13: ...

  2. 【题解】【BST】【Leetcode】Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. #include #import @class 的一些用法区别

    从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...

  4. Notes of Principles of Parallel Programming - TODO

    0.1 TopicNotes of Lin C., Snyder L.. Principles of Parallel Programming. Beijing: China Machine Pres ...

  5. 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. JavaScript HTML DOM---遗漏知识再整理(向html添加/删除元素,改变内容和css)

    1.  HTML DOM 改变 HTML 内容:(HTML DOM 允许 JavaScript 改变 HTML 元素的内容.) (1)改变 HTML 输出流 在 JavaScript 中,docume ...

  7. UVa 814邮件传输代理的交互

    好吧,终于知道原来string有这么多用法,map可以通过vector来映射多个数 #include <iostream> #include <string> #include ...

  8. UVa 400

    一开始没怎么看懂题目,原来就是M字符就是这一列的宽度为M个字符,包括空格. #include<iostream> #include<algorithm> #include< ...

  9. phpwind将服务器数据同步到本地之后网站不显示或者排版错误

    在将phpwind的数据同步到本地服务器之后 如果访问本地服务器的首页不能显示的话 首先要查看global.php文件中的D_P变量,官方默认 的此变量应该指向和R_P变量是同一个文件夹即网站的根目录 ...

  10. ExtJS控件样式修改及美化

    Extjs项目对富客户端开发提供了强有力的支持,甚至改变了前端的开发方式,使得开发变得更加趋向于“面向组件”.对界面的美化而言,也是根本性的改变.普通的网页美工面对extjs项目根本无法下手,需要脚本 ...