随笔 - 167  文章 - 1  评论 - 12  阅读 - 16万

【原】ios tableViewCell 自适应高度

原文:http://www.cnblogs.com/A--G/p/4819051.html

  前言:之前在做一个类似微博的小需求时候,用table view实现了微博文字和图片等等的基本展示,由于文字和图片的数量问题,cell高度会受到影响。

 

以前的做法是在heightForRowAtIndexPath 里取出这条微博的model,计算图片和文字的高度,现在有一个简单一点的办法:

 

在heightForRowAtIndexPath里重新获取这个cell的高度即可,

    TableViewCell *cell =  (TableViewCell *)[self tableView:self.table cellForRowAtIndexPath:indexPath];
    
    return cell.frame.size.height;

试验发现,需要注意一个前提,在刷新data,也就是在viewController里的cellForRowAtIndexPath中,如果不刷新cell的frame,则默认是44,所以,你刷新data的计算好cell高度的时候,必须要刷新cell的frame

 

posted on   MichaelMao  阅读(823)  评论(0编辑  收藏  举报
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示