转:动态计算UITableViewCell高度详解




- UINib *cellNib = [UINib nibWithNibName:@"C1" bundle:nil];
- [self.tableView registerNib:cellNib forCellReuseIdentifier:@"C1"];
- self.tableData = @[@"1\n2\n3\n4\n5\n6", @"123456789012345678901234567890", @"1\n2", @"1\n2\n3", @"1"];
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // Return the number of rows in the section.
- return self.tableData.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C1 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- return cell;
- }
- @property (nonatomic, strong) UITableViewCell *prototypeCell;
- self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"C1"];
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C1 *cell = (C1 *)self.prototypeCell;
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
- NSLog(@"h=%f", size.height + 1);
- return 1 + size.height;
- }




- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C2 *cell = (C2 *)self.prototypeCell;
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
- CGSize textViewSize = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat h = size.height + textViewSize.height;
- h = h > 89 ? h : 89; //89是图片显示的最低高度, 见xib
- NSLog(@"h=%f", h);
- return 1 + h;
- }


- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C3 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C3"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- [cell.t sizeToFit];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C3 *cell = (C3 *)self.prototypeCell;
- NSString *str = [self.tableData objectAtIndex:indexPath.row];
- cell.t.text = str;
- CGSize s = [str calculateSize:CGSizeMake(cell.t.frame.size.width, FLT_MAX) font:cell.t.font];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- NSLog(@"h=%f", height);
- return 1 + height;
- }
- - (CGSize)calculateSize:(CGSize)size font:(UIFont *)font {
- CGSize expectedLabelSize = CGSizeZero;
- if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
- NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
- expectedLabelSize = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
- }
- else {
- expectedLabelSize = [self sizeWithFont:font
- constrainedToSize:size
- lineBreakMode:NSLineBreakByWordWrapping];
- }
- return CGSizeMake(ceil(expectedLabelSize.width), ceil(expectedLabelSize.height));
- }


- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C4 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C4"];
- cell.t.text = [self.tableData objectAtIndex:indexPath.row];
- [cell.t sizeToFit];
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C4 *cell = (C4 *)self.prototypeCell;
- NSString *str = [self.tableData objectAtIndex:indexPath.row];
- cell.t.text = str;
- CGSize s = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- return 1 + height;
- }


- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- C5 *cell = [self.tableView dequeueReusableCellWithIdentifier:@"C5"];
- cell.t.text = @"123";
- cell.t.delegate = self;
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- C5 *cell = (C5 *)self.prototypeCell;
- cell.t.text = self.updatedStr;
- CGSize s = [cell.t sizeThatFits:CGSizeMake(cell.t.frame.size.width, FLT_MAX)];
- CGFloat defaultHeight = cell.contentView.frame.size.height;
- CGFloat height = s.height > defaultHeight ? s.height : defaultHeight;
- return 1 + height;
- }
- #pragma mark - UITextViewDelegate
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- if ([text isEqualToString:@"\n"]) {
- NSLog(@"h=%f", textView.contentSize.height);
- }
- return YES;
- }
- - (void)textViewDidChange:(UITextView *)textView {
- self.updatedStr = textView.text;
- [self.tableView beginUpdates];
- [self.tableView endUpdates];
- }
转:动态计算UITableViewCell高度详解的更多相关文章
- 动态计算UITableViewCell高度详解 (转)
感觉挺有用的一篇文章,分析了4种解决方案.回头测试之.如果有别的方案,我会在后面补上. 原文地址:http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan ...
- 动态计算UITableViewCell高度详解
本文将介绍四种情况下UITableViewCell的计算方式,分别是: Auto Layout with UILabel in UITableViewCell Auto Layout with UIT ...
- 动态计算UITableViewCell高度
动态计算UITableViewCell高度 UILabel in UITableViewCell Auto Layout - UILabel的属性Lines设为了0表示显示多行.Auto Layout ...
- 动态调整UITableViewCell高度的实现方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- IOS中UITableViewCell使用详解
IOS中UITableViewCell使用详解 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(N ...
- JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解
在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...
- 动态计算Label高度
//1.设置该label的numberOfLines为0 self.titleLabel.numberOfLines = 0; //2.字体的设置要与之前相同 NSDictionary * at ...
- 大数据入门第十六天——流式计算之storm详解(一)入门与集群安装
一.概述 今天起就正式进入了流式计算.这里先解释一下流式计算的概念 离线计算 离线计算:批量获取数据.批量传输数据.周期性批量计算数据.数据展示 代表技术:Sqoop批量导入数据.HDFS批量存储数据 ...
- 静态代理,动态代理,Cglib代理详解
一.静态代理 新建一个接口 定义一个玩家方法: package com."".proxy.staticc; public interface Iplayer { public vo ...
随机推荐
- 使用python快速搭建web服务器
命令:python -m SimpleHTTPServer 8088 参考:https://www.cnblogs.com/harry-xiaojun/p/6739003.html https://w ...
- Python对list去重
Python对list去重 方法一 新建新的列表,利用not in命令去重.这种方法看起来不够简便,但是保留了原列表中的顺序.代码如下: list1 = [1,2,3,4,1,1,2,5,4,3] l ...
- GIT的使用及心得
先给大家个很棒的GIT使用教程链接http://blog.jobbole.com/78960/ 这个链接是我在学习使用GIT时看的教程博客,分享给大家,里面还有介绍SVN与GIT的区别 GITHUB上 ...
- Java中DAO的实现
J2EE 开发人员使用数据访问对象(Data Access Object DAO)设计模式,以便将低级别的数据访问逻辑与高级别的业务逻辑分离.实现 DAO 模式涉及比编写数据访问代码更多的内容.在本文 ...
- vue-cli 安装时 npm 报错 errno -4048
如何解决vue-cli 安装时 npm 报错 errno -4048 第一种解决方法:以管理身份运行cmd.exe 第二种解决办法:在dos窗口输入命令 npm cache clean --fo ...
- Scrum Meeting Beta - 6
Scrum Meeting Beta - 6 NewTeam 2017/12/5 地点:主南201 任务反馈 团队成员 完成任务 计划任务 安万贺 完成了离线状态本地存储的读取Issue #133Pu ...
- [转帖] Oracle JDK 11 正式发布.. 版本号真快
Java 11 / JDK 11 正式发布! oschina 发布于 2018年09月26日 收藏 19 评论 38 在您的既有IT基础设施上按需构建人工智能更高效>>> 美 ...
- Window系统 安装TFLearn
1. 确保成功安装了tensorflow 2. 查看当前tensorflow下的库文件,判断是否已经安装了h5py,scipy:conda list 3. 若没有安装,安装h5py,scipy.我的电 ...
- MySQL 测试工具(基准测试、压力测试)
1.sysbench sysbench是跨平台的基准测试工具,支持多线程,支持多种数据库: 主要包括以下几种测试: cpu性能 磁盘io性能 调度程序性能 内存分配及传输速度 POSIX线程性能 数据 ...
- winrar 授权破解过期解决
RAR registration data Federal Agency for Education 1000000 PC usage license UID=b621cca9a84bc5deffbf ...