动态计算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

相关文章
|
前端开发 JavaScript 容器
overflow和动态计算高度
overflow和动态计算高度
88 0
SwiftUI—创建一个水平方向上的滚动视图
SwiftUI—创建一个水平方向上的滚动视图
210 0
SwiftUI—创建一个水平方向上的滚动视图
SwiftUI—使用HStack在水平方向排列视图
SwiftUI—使用HStack在水平方向排列视图
650 0
SwiftUI—使用HStack在水平方向排列视图
SwiftUI—使用ZStack在深度方向排列视图
SwiftUI—使用ZStack在深度方向排列视图
214 0
SwiftUI—使用ZStack在深度方向排列视图
QSpinBox/QDoubleSpinBox/QSlider组合使用
QSpinBox/QDoubleSpinBox/QSlider组合使用
317 0
自定义的 ListBoxItem 自适应ListBox的宽度
原文:自定义的 ListBoxItem 自适应ListBox的宽度    主要是要设置HorizontalContentAlignment的值,而不是HorizontalAlignment 1 2 3 4 5 6 ...
955 0