设置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 ...
随机推荐
- 使用了未经检查或不安全的操作。有关详细信息, 请使用 -Xlint:unchecked 重新编译。
警告信息如下:
- [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂
Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...
- NodeJS学习笔记 (7)网络服务-http-client(ok)
原文:https://github.com/chyingp/nodejs-learning-guide 自己敲代码: ClientRequest概览 当你调用 http.request(options ...
- element-ui的table表格控件表头与内容列不对齐问题
原文链接:点我 element-ui的table表格控件表头与内容列不对齐问题 解决方法:将以下样式代码添加到index.html.或app.vue中(必须是入口文件,起全局作用!)body .el- ...
- 学习《数据科学入门》中文PDF+英文PDF+源代码
数据科学是一个蓬勃发展.前途无限的行业,有人将数据科学家称为"21世纪头号性感职业".本书从零开始讲解数据科学工作,教授数据科学工作所必需的黑客技能,并带领读者熟悉数据科学的核心知 ...
- 紫书 习题 8-2 UVa 1610 (暴力出奇迹)
这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...
- 洛谷 P1275 魔板
P1275 魔板 题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状 ...
- Android之——拦截短信
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46994097 这里.向大家简介通过BroadcastReceiver来拦截短信的方 ...
- (三)ng-app的使用困惑和angularJS框架的自己主动载入
ng-app是angular的一个指令,代表一个angular应用(也叫模块).使用ng-app或ng-app=""来标记一个DOM结点.让框架会自己主动载入.也就是说,ng-ap ...
- [Transducer] Create a Sequence Helper to Transduce Without Changing Collection Types
A frequent use case when transducing is to apply a transformation to items without changing the type ...