动态计算UITableviewcell高度

简介: 在iOS开发中,我们少不了和UITableview打交道,因为UITableview也是UIKit中最复杂的一个控件了。在使用UITableview的过程中,UITableviewCell也是必不可少的,页面列表形式的展示可谓是各种各样,相信不少童鞋们也曾为复杂的页面布局困惑过,其中比较难的也就数cell的高度自适应了,也就是说cell的高度是根据内容来动态计算的。

在iOS开发中,我们少不了和UITableview打交道,因为UITableview也是UIKit中最复杂的一个控件了。在使用UITableview的过程中,UITableviewCell也是必不可少的,页面列表形式的展示可谓是各种各样,相信不少童鞋们也曾为复杂的页面布局困惑过,其中比较难的也就数cell的高度自适应了,也就是说cell的高度是根据内容来动态计算的。


1.不使用Autolayout的时候,计算cell的高度:

//返回cell的的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoInfo *photoInfo = [self.dataArr objectAtIndex:indexPath.row];
    [self heightForRowWithModel:photoInfo];
}
//动态计算cell的高度 
- (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo 
{ 
    //这里只写了label的计算
    //文本的高度 
    CGSize texSize = [self labelAutoCalculateRectWith:photoInfo.instruction FontSize:15 MaxSize:CGSizeMake(200,1000)];
    //3.返回cell 的总高度 
    return 44 + textSize.height;
}  
/*根据传过来的文字内容、字体大小、宽度和最大尺寸动态计算文字所占用的size
              * text 文本内容 
              * fontSize 字体大小
              * maxSize  size(宽度,1000)
              * return  size (计算的size)
              */
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
   paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize;
      //如果是IOS6.0
    if (![text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]){
        labelSize = [text sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:maxSize lineBreakMode:NSLineBreakByWordWrapping];
    }
    //如果系统为iOS7.0
    else
  {
          // iOS7中用以下方法替代过时的iOS6中的sizeWithFont:constrainedToSize:lineBreakMode:方法
        labelSize = [text boundingRectWithSize: maxSize
                                       options: NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine
                                    attributes:attributes
                                       context:nil].size;
    }
    labelSize.height=ceil(labelSize.height);    
    labelSize.width=ceil(labelSize.width);
    return labelSize;
}


2.使用Autolayout,- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize这个方法就能搞定。不过,首先要在Xib上布局cell。


image.png

//返回cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMSGTableViewCell *cell = self.cell;
    SystemmsgInfo *data = self.dataArr[indexPath.row];
    CGFloat height = [cell heightForCell:data];
    return height;
} 
//动态计算cell的高度
- (CGFloat)heightForCell:(SystemmsgInfo *)data
{
    self.widthLabel.constant = ScreenWidth - 40;
    self.contentLabel.text = data.promotionInfo;
    CGSize size = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    NSLog(@"h=%f", size.height + 1);
    return 1  + size.height;
}


image.png

目录
打赏
0
0
0
0
2
分享
相关文章
|
4月前
在使用 Flexbox 布局实现自适应宽度的品字布局时,子元素的排列顺序是否可以调整?
【10月更文挑战第27天】使用Flexbox布局实现自适应宽度的品字布局时,可以通过调整HTML结构中的顺序或使用 `order` 属性来灵活地调整子元素的排列顺序,以满足不同的设计和布局需求。
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
canvas系列教程04 —— 渐变、阴影、路径、状态、Canvas对象、图形重叠模式
618 0
百度地图开发:批量增加折线、多边形覆盖物的封装函数
百度地图开发:批量增加折线、多边形覆盖物的封装函数
120 0
UITableViewCell布局里面文字的自适应
UITableViewCell布局里面文字的自适应
180 0
UITableViewCell布局里面文字的自适应
div里面的元素在【垂直 方向】上水平分布 使用calc()函数动态计算
div里面的元素在【垂直 方向】上水平分布 使用calc()函数动态计算
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等