用auto layout 来动态计算cell Height.

文章的链接如下:http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

1.文章的思路如下,先为每个UI元素做好autolayout.

2. 在 tableView:heightForRowAtIndexPath: 方法中,重新为每个cell重新填充实际内容,以后调用“

[sizingCell setNeedsLayout];
  [sizingCell layoutIfNeeded];
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

去返回实际高度。


文章还讲到一个很重要的方法

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
感觉多数还是用来lazy load的功能,可以提升性能,缺点也不少下面讲的很好。

Pro tip: You can greatly improve the performance of a table view that has many cells by implementing tableView:estimatedHeightForRowAtIndexPath:.

However, be warned that this delegate method comes with its own set of caveats.

If your cell estimates are inaccurate, then scrolling might be jumpy, the scroll indicator may be misleading, or the content offset may get messed up. For example, if the user rotates the device or taps the status bar to scroll to the top, the result could be a gap at the top/bottom of the table view.

If your table view has only a few cells, or if your cell heights are difficult to estimate in advance, you can opt to skip implementation of this method. However, you won’t have to worry about these issues.

If you notice poor loading, reloading, or scrolling, try implementing this method.

The key to success is finding a good balance between accurately estimating cell height and the performance cost of such calculation.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值