美文网首页
IOS TableViewCell高度计算

IOS TableViewCell高度计算

作者: Yumazhiyao | 来源:发表于2016-07-28 11:46 被阅读152次

第一种:根据文字内容设置估算高度自动计算

1.自定义cell
2.在cell里添加Label,设置Label上边距和下边距,设置自动换行,宽度用代码设置

contentLabelWCons.constant = UIScreen.mainScreen().bounds.width - 2 * edgeMargin

然后设置

  //高度自动计算
        tableView.rowHeight = UITableViewAutomaticDimension
  //估算高度
        tableView.estimatedRowHeight = 200

第二种:根据文字所在范围,计算

1.自定义cell
2.在cell里添加Label,设置Label上边距和下边距和#宽度约束#,设置自动换行

  //确定范围
       CGSize textSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 4 * MAXMargin, MAXFLOAT);

  //这里的字体设置要和Xib里边Label设置的字体一致
        CGFloat textH = [self.text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil].size.height;

3.再实现一个代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MAXContent *c = self.contents[indexPath.row];
    return c.cellH;
}

相关文章

网友评论

      本文标题:IOS TableViewCell高度计算

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