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

相关文章
|
4月前
【sgDragMove】自定义组件:自定义拖拽组件,仅支持拖拽、设置吸附屏幕边界距离。
【sgDragMove】自定义组件:自定义拖拽组件,仅支持拖拽、设置吸附屏幕边界距离。
SwiftUI—创建一个水平方向上的滚动视图
SwiftUI—创建一个水平方向上的滚动视图
197 0
SwiftUI—创建一个水平方向上的滚动视图
UITableViewCell布局里面文字的自适应
UITableViewCell布局里面文字的自适应
153 0
UITableViewCell布局里面文字的自适应
|
API
[微信小程序]通过计算其他view的高度,动态给定scroll-view的高度
WXML节点信息API 微信小程序的开发文档有个很重要的api wx.createSelectorQuery() 具体大家还是看一下文档,我下面是直接上代码解说; wx.createSelectorQuery()文档 案例中的布局 这里页面上部分有三个view,它们的class分别是.
7247 0