文章的链接如下: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.