这里介绍Self Sizing Cells的相关设置和应用
Self Sizing Cells是iOS8之后推出的新特性,帮助开发者自动计算cell的高度,减少开发复杂度。
Self Sizing Cells的实现依赖于auto layout, 系统会自动根据用户定义的约束来计算cell最合适的高度。
实现起来很简单:

1.从containtView的 top edge到bottom edge定义好所有约束,中间的约束不要断掉 (记得UILabel设置多行(行数为0))
2.在viewDidLoad()中添加:(要使用Self Sizing Cells必须给estimatedRowHeight设置一个值,并且rowHeight设为UITableViewAutomaticDimension,当这两个都设置好之后,系统就会自动根据约束去计算高度。)
mainTableView.estimatedRowHeight=50.0 //这里最好是prototype中cell的高度,越精确,系统计算的高度越精确。
mainTableView.rowHeight=UITableViewAutomaticDimension
使用Self Sizing Cells之前:

使用Self Sizing Cells之后:

demo 地址:SelfSizingCellsDemo
网友评论