美文网首页
cell 自适应高度

cell 自适应高度

作者: 我有一只小毛驴_从来都不骑 | 来源:发表于2016-12-17 13:34 被阅读16次

    cell自适应高度

    1.- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath里直接算出cell的高度
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * 10, MAXFLOAT);
    // CGFloat textH = [topic.text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:maxSize].height;//方法弃用
    CGFloat textH = [topic.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;

    每加载一次````cell```` 就算一次,如果````cell````比较多  就会出现偶尔卡顿的情况
    

    2.把计算cell 的高度写 到模型里 这样 cell的高度酒只算了一次
    - (CGFloat)cellHeight
    {
    if (!_cellHeight) {
    // 文字的最大尺寸
    CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * XMGTopicCellMargin, MAXFLOAT);
    // 计算文字的高度
    CGFloat textH = [self.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;

        // cell的高度
        _cellHeight = XMGTopicCellTextY + textH + XMGTopicCellBottomBarH + 2 * XMGTopicCellMargin;
    }
    return _cellHeight;
    

    }

    还有一种更简单的方法
    
    1. label 的约束必须要有距离底部的高度
    2. self.tabView.estimatedRowHeight = 40;//必须要有估计高度
    3. self.tabView.rowHeight = UITableViewAutomaticDimension;
    这是iOS8.0 以后的方法  适用于 所有的cell 都一样  那种

    相关文章

      网友评论

          本文标题:cell 自适应高度

          本文链接:https://www.haomeiwen.com/subject/avvomttx.html