转:动态计算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 ...
随机推荐
- 图文转换NABCD
作为图文转化还是有很多优点的,在这里我就分析一下它的方便快捷 Need:有些非电子版的文字不方便我们编辑,图文转换可以轻而易举达到目的. Approach:现在技术手段应该还有点难度,应该可以换个方法 ...
- 团队作业7——第二次项目冲刺(Beta版本12.04——12.07)
1.当天站立式会议照片 本次会议在5号公寓3楼召开,本次会议内容:①:熟悉每个人想做的模块.②:根据项目要求还没做的完成. 2.每个人的工作 经过会议讨论后确定了每个人的分工 组员 任务 陈福鹏 实现 ...
- Hadoop到底能做什么?怎么用hadoop?
hadoop是什么?(1)Hadoop是一个开源的框架,可编写和运行分布式应用处理大规模数据,是专为离线和大规模数据分析而设计的,并不适合那种对几个记录随机读写的在线事务处理模式.Hadoop=HDF ...
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- 初期测评 A 排序
https://vjudge.net/contest/240302#problem/A 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0 ...
- windwon安装macaca环境
一 安装配置java 1.安装java_jdk ,安装过程中顺带一起安装jre (1)选择[新建系统变量]--弹出“新建系统变量”对话框,在“变量名”文本框输入“JAVA_HOME”,在“ ...
- LR_问题_虚拟用户以进程和线程模式运行的区别
进程方式和线程方式的优缺点: 如果选择按照进程方式运行, 每个用户都将启动一个mmdrv进程,多个mmdrv进程会占用大量内存及其他系统资源,这就限制了可以在任一负载生成器上运行的并发用户数的数量,因 ...
- golang build 的简单用法.(菜鸟初学)
1. golang 里面的 go build 可以编译代码. go build helloworld.go 2. 这里面有一个注意事项事项. 如果引用非go语言的 内置package的话 需要在环境变 ...
- Windows安装ElastAlert问题总结
1.运行时出现UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xb4 in position 0: invalid start byte 或 ...
- 时空KSOA之CS表单工具说明
CS表单工具说明 1.调用: 1.1.单据事件调用 runbill_表单sn 调用无窗口表单 loadbill_表单sn 调用窗口表单 1.2.功能调用 功能号:LOADCSBILL 参数表单名称 1 ...