有非常多时候。UITableViewCell每行的高度是不固定的,须要动态设置。

UITableView有个代理方法,

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ((CZWeiboFrame*)[self.weiboFrames objectAtIndex:indexPath.row]).rowHeight; }

有的会想在这里先获取cell。然后过去cell的高度再返回,可是这是不可行的,由于上述方法会在

下述方法之前运行。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CZWeiboFrame *model = self.weiboFrames[indexPath.row] ;

    static NSString *identifer = @"weibo";
CZWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
if(cell == nil){
cell = [[CZWeiboCell alloc ] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer]; }
cell.weiboFrame = model; return cell;
}

也就是说在你获取cell之前必须先设置cell的高度。

那么该怎么做呢。那就得在你给cell设置数据时,如今数据模型中计算出高度。

以下是上述代码中CZWeiboViewCell模型的结构

@interface CZWeiboFrame : NSObject

@property   (nonatomic,strong) CZWeibo *weibo;
@property (nonatomic,assign,readonly) CGRect iconFrame;
@property (nonatomic,assign,readonly) CGRect textFrame;
@property (nonatomic,assign,readonly) CGRect nameFrame;
@property (nonatomic,assign,readonly) CGRect pictureFrame;
@property (nonatomic,assign,readonly) CGRect vipFrame;
@property (nonatomic,assign,readonly) CGFloat rowHeight; @end

当中weibo是每行要显示的数据。row height是行高,其余是cell中每一个控件的frame,在weibo的set方法中计算frame

-(void)setWeibo:(CZWeibo *)weibo{
_weibo = weibo; CGFloat margin = 10;
CGFloat iconW = 35;
CGFloat iconH = 35;
CGFloat iconX = margin;
CGFloat iconY = margin;
_iconFrame = CGRectMake(iconX, iconY, iconW, iconH); CGFloat nameX = CGRectGetMaxX(_iconFrame)+margin;
//依据Labele中文字的内容动态计算大小
//头文件NSAttributString
NSDictionary *attr = @{NSFontAttributeName:nameFont};
CGSize nameSize = [self sizeWithText:_weibo.name size:CGSizeMake(MAXFLOAT, MAXFLOAT) font:nameFont];
CGFloat nameW = nameSize.width;
CGFloat nameH = nameSize.height;
CGFloat nameY = iconY + (iconH - nameH)/2; _nameFrame = CGRectMake(nameX, nameY, nameW, nameH); CGFloat vipW = 10;
CGFloat vipH = 10;
CGFloat vipX = CGRectGetMaxX(_nameFrame)+margin;
CGFloat vipY = iconY +(iconH - vipH)/2;
_vipFrame = CGRectMake(vipX, vipY, vipW, vipH); CGFloat textX = iconX;
CGFloat textY = CGRectGetMaxY(_iconFrame)+margin;
CGSize s = CGSizeMake([[UIScreen mainScreen] bounds].size.width - margin*2, MAXFLOAT);
CGSize textSize = [self sizeWithText:weibo.text size:s font:textFont]; _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height); CGFloat picW = 200;
CGFloat picH = 200;
CGFloat picX = iconX;
CGFloat picY = CGRectGetMaxY(_textFrame)+margin;
_pictureFrame = CGRectMake(picX, picY, picW, picH) ; _rowHeight = 0;
if(self.weibo.picture){
_rowHeight = CGRectGetMaxY(_pictureFrame)+margin;
}else{
_rowHeight = CGRectGetMaxY(_textFrame)+margin;
}
}
///获取字符串的size
-(CGSize) sizeWithText:(NSString *) text size:(CGSize) size font:(UIFont *)font{
NSDictionary *dict = @{NSFontAttributeName:font };
CGSize sz = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
return sz;
}

然后我们就能给每一行设置高度了

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return ((CZWeiboFrame*)[self.weiboFrames objectAtIndex:indexPath.row]).rowHeight;<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
}

设置UITableViewCell高度的问题的更多相关文章

  1. 优化UITableViewCell高度计算的那些事

    优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...

  2. uitableviewcell高度自适应笔记

    今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cel ...

  3. UITableViewCell高度自适应探索--AutoLayout结合Frame

    UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...

  4. 优化UITableViewCell高度计算的那些事(RunLoop)

    这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...

  5. UITableViewCell 高度计算从混沌初始到天地交泰

    [原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...

  6. 《转》优化UITableViewCell高度计算的那些事

    我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...

  7. 转:动态计算UITableViewCell高度详解

    转自:http://www.cocoachina.com/industry/20140604/8668.html   不知道大家有没有发现,在iOS APP开发过程中,UITableView是我们显示 ...

  8. 动态计算UITableViewCell高度详解

    本文将介绍四种情况下UITableViewCell的计算方式,分别是: Auto Layout with UILabel in UITableViewCell Auto Layout with UIT ...

  9. 动态计算UITableViewCell高度详解 (转)

    感觉挺有用的一篇文章,分析了4种解决方案.回头测试之.如果有别的方案,我会在后面补上. 原文地址:http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan ...

随机推荐

  1. category和关联对象

    如上所见,我们知道在category里面是无法为category添加实例变量的.但是我们很多时候需要在category中添加和对象关联的值,这个时候可以求助关联对象来实现. MyClass+Categ ...

  2. 洛谷P3355 骑士共存问题 二分图_网络流

    Code: #include<cstdio> #include<cstring> #include<queue> #include<vector> #i ...

  3. React diff机制(介绍虚拟DOM的机制)

    https://segmentfault.com/a/1190000004003055

  4. POJ-2420 A Star not a Tree? 梯度下降 | 模拟退火

    题目链接:https://cn.vjudge.net/problem/POJ-2420 题意 给出n个点,找一个点,使得这个点到其余所有点距离之和最小. 思路 一开始就在抖机灵考虑梯度下降,猜测是个凸 ...

  5. LightOJ-1074 Extended Traffic 最短路问题 注意连通性

    题目链接:https://cn.vjudge.net/problem/LightOJ-1074 题意 给一图 求最短路 若最短路<3或没有最短路,则输出'?' 思路 首先注意到可能存在负环,所以 ...

  6. 如何绑定host

    绑定host一般分为windows和linux下两种情况. windows下,首先打开host文件,其操作步骤 ,打开 C:\Windows\System32\Drivers\etc\hosts的文件 ...

  7. HTTP——状态码

    (转载) 完整的 HTTP 1.1规范说明书来自于RFC 2616,你可以在http://www.talentdigger.cn/home/link.php?url=d3d3LnJmYy1lZGl0b ...

  8. securefx连接linux后文件夹中文乱码问题解决

    首先在选项中设置字符编码为UTF-8 然后在全局选项中找到Securefx的配置文件 进入到该目录中,选择“Sessions”: 在“Sessions”中找到链接地址的ini文件,并用文本编辑器打开: ...

  9. hadoop-13-root ssh无密码登陆

    hadoop-13-root ssh无密码登陆 生产机器禁止ROOT远程SSH登录: vi /etc/ssh/sshd_config 把 PermitRootLogin yes 改为 PermitRo ...

  10. 【HDU-4614】Vases and Flowers(线段树双查询)

    11946317 2014-10-23 09:08:28 Accepted 4614 437MS 2348K rid=11946317" target="_blank" ...