设置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 ...
随机推荐
- swift语言点评十五-$0
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...
- 原生ajax实现文件上传
视图层 JS 函数: <input type="file" onchange="sendFile()" id="up" /> ...
- [zjoi2016]小星星 (容斥+DP)
我们先用树形DP,求出选取集合S中的点,满足连通性的但是标号可重的方案数,贡献给F(i)(1\(\leq\)i\(\leq\)\(\mid S\mid\)),也就是我们要处理出F(i)代表取至多i个点 ...
- [Vijos P1369]难解的问题
题目大意:给你一个序列,叫你求最长上升子序列长度,但必须包含第k项. 解题思路:我们把k左边的比a[k]大的数去掉,k右边的比k小的数去掉,就可以保证选到a[k]了(因为左边的数小于a[k],而a[k ...
- jsp页面跳转的路径问题
<form class="box login" action="/graduation_system/BServlet" method="pos ...
- 面试题——ArrayList和LinkedList的区别
List概括 先回顾一下List在Collection的框架图: 从图中可以看出: List是一个接口,他继承Collection接口,代表有序的队列. AbstractList是一个抽象类, ,它继 ...
- sqoop从mysql导入到hdfs出现乱码问题
最近把hive元数据库的快照数据导入到hdfs中,以便对历史的元数据进行查询. 命令如下: sqoop import -D mapred.job.queue.name=do.production -- ...
- 架构设计--用户端全http參数接口具体说明v1
1. 用户端全http參数接口具体说明v1.doc 1 2. change histor 1 3. 接口通用參数说明 1 4. 函数注冊接口(规划中) 3 5. 用户权限模块 3 5.1. 用户注冊接 ...
- HDU 5297 Y sequence Y数列
题意:给定正整数n和r.定义Y数列为从正整数序列中删除全部能表示成a^b(2 ≤ b ≤ r)的数后的数列,求Y数列的第n个数是多少. 比如n = 10. r = 3,则Y数列为2 3 5 6 7 1 ...
- CMD应用 qtp/winshell/cmd的交互
=================================================================== '採用windows.shell的 sendkeys 方式: s ...