设置UITableViewCell高度的问题
有非常多时候。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高度的问题的更多相关文章
- 优化UITableViewCell高度计算的那些事
优化UITableViewCell高度计算的那些事 我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化 ...
- uitableviewcell高度自适应笔记
今天看了几篇uitableviewcell高度自适应的文章,大体分为两种方式. 第一种方式,cell里面有label,在cellforrow绘制的时候计算Label的可能高度,并且在此时重新计算cel ...
- UITableViewCell高度自适应探索--AutoLayout结合Frame
UITableViewCell高度自适应探索--UITableView+FDTemplateLayoutCell地址: http://www.jianshu.com/p/7839e3a273a6UIT ...
- 优化UITableViewCell高度计算的那些事(RunLoop)
这篇总结你可以读到: UITableView高度计算和估算的机制 不同iOS系统在高度计算上的差异 iOS8 self-sizing cell UITableView+FDTemplateLayout ...
- UITableViewCell 高度计算从混沌初始到天地交泰
[原创]UITableViewCell 高度计算从混沌初始到天地交泰 本文主要基予iOS UITableViewCell 高度自适应计算问题展开陈述,废话少说直入正题: UITableView控件可能 ...
- 《转》优化UITableViewCell高度计算的那些事
我是前言 这篇文章是我和我们团队最近对 UITableViewCell 利用 AutoLayout 自动高度计算和 UITableView 滑动优化的一个总结.我们也在维护一个开源的扩展,UITabl ...
- 转:动态计算UITableViewCell高度详解
转自:http://www.cocoachina.com/industry/20140604/8668.html 不知道大家有没有发现,在iOS APP开发过程中,UITableView是我们显示 ...
- 动态计算UITableViewCell高度详解
本文将介绍四种情况下UITableViewCell的计算方式,分别是: Auto Layout with UILabel in UITableViewCell Auto Layout with UIT ...
- 动态计算UITableViewCell高度详解 (转)
感觉挺有用的一篇文章,分析了4种解决方案.回头测试之.如果有别的方案,我会在后面补上. 原文地址:http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan ...
随机推荐
- 初识Git(三)
这次要记录一下对branch,merge的学习. 与先前一样创建一个pro文件夹,initi该文件夹,在该文件夹中新建一个空的MainCode.txt,然后add文本文件并且commit. 接下来我们 ...
- 一个php处理图片裁剪,压缩,水印的小代码
插件地址:https://github.com/cigua/imagefilter
- plsql 查询历史执行语句
control+e. 如果执行删除.修改.增加的操作,未提交的历史记录中也有.
- 洛谷 P1462 通往奥格瑞玛的道路 二分 最短路
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using ...
- Java线程之基础
Java内存模型(jmm) 线程通信 消息传递 重排序 顺序一致性 Happens-Before As-If-Serial 一.线程的生命周期及五种基本状态 线程生命周期:新建.就绪.运行.阻塞.死亡 ...
- 由Request Method:OPTIONS初窥CORS(转)
刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS…… 目前的工作中,HEAD.PUT.DELE ...
- 注解形式读取properties文件中的属性
1.spring.xml中加入(多个properties 用逗号隔开) <context:property-placeholder location="classpath:jdbc. ...
- linux 连接 NAS
[root@kvm-server ~]# mount -o username=user01,password=1234567890 //192.168.31.20/share /mnt/nas Cou ...
- OpenJudge 1031 Lausanne Capitale Olympique
Lausanne Capitale Olympique Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on ...
- servlet3.0 @webfilter 过滤顺序
Servlet3.0之前Filter过滤的顺序是由用户在web.xml中配置的顺序决定的,如下会先执行encodingFilter,再执行filter1. <filter> <dis ...