UITableViewCell 复杂时,高度计算优
来源:互联网 发布:最新淘宝小模特征集 编辑:程序博客网 时间:2024/04/27 20:27
方法1:简单粗暴,利用新的api接口 systemLayoutSizeFittingSize
我们声明一个存计算Cell高度的实例变量:
- @property (nonatomic, strong) UITableViewCell *prototypeCell;
然后初始化它:
- self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:@"C1" ];
实现tableView的高度代理
- - (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];
- 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
- return 1 + h;
- }
要求返回一个Cell的估计值,
(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath。
方法优化1:cell使用属性保存,调用方便。并且防止计算cell高度时重复创建cell实例。
优化2,cell的高度通过新api计算cell.contentView自动适配后的的高度然后加1。
优化3,避免反复调用多次heightForRowAtIndexPath
优化4,sizeThatFits优化计算uitextview的高度
主要应用:Auto Layout with UILabel UITextView in UITableViewCell
方法2:灵活巧用CGSIZE
- - (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;
- }
这儿用到了一个NSString的Cagetory方法:
- - (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));
- }
使用到的方法 sizeToFit,boundingRectWithSize,sizeWithFont
0 0
- UITableViewCell 复杂时,高度计算优
- 使用Autolayout时UITableViewCell的高度计算
- 计算UITableViewCell高度
- UITableViewCell动态计算高度
- 优化UITableViewCell高度计算
- 动态计算UITableviewCell高度
- 计算UITableViewCell高度
- UITableViewCell高度计算
- UITableViewCell高度计算
- 优化UITableViewCell高度计算
- 优化UITableViewCell高度计算
- 优化UITableViewCell高度计算
- 有关UITableViewCell高度计算
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- C++中二维数组的参数传递
- C/C++连接MySql数据库
- Git开篇:认识Git
- JavaScript进阶基础二
- ZOJ 题目分类
- UITableViewCell 复杂时,高度计算优
- java变量所占字节数及取值范围
- 页面里的在线实时聊天实现
- 第十天 权重
- The working copy is locked due to a previous error
- android系统方法裁剪图片 华为手机显示为圆
- iOS 不要用宏来定义你的常量
- 数组 bitmap 适配器
- 【51单片机】蜂鸣器