UITableView使用AutoLayout实现动态高度
2017-12-20 本文已影响23人
梦翔_d674
最早实现Cell动态高度使用单个元素高度计算在相加出现多行UILabel用计算文本所需高度计算,准确度不高。
计算高度代码后来使用预估高度和AutoLayout手动计算Cell高度并缓存
Controller代码 Cell代码最后使用预估高度和AutoLayout自动计算Cell高度,是不是很简单,这里有个坑cell高度使用UITableViewAutomaticDimension sectionHeader 和sectionFooter 高度也需要使用UITableViewAutoMaticDimension 否则类似聊天界面会出现跳屏现象 ,如果出现不换行现象可以设置label的preferredMaxLayoutWidth 最大宽度值 和[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical] 这个方法