动态单元格高度的自动布局约束条件
问题描述:
我一直在从堆栈溢出中搜索很久,发现了一些相关的问题,但并不完美。 有人可以解释详细说明什么是使标签增长的高度基于tableview单元格中的文本。我想增加标签高度,然后单元格高度。 谢谢。动态单元格高度的自动布局约束条件
答
对于iOS 8+,您可以找到解释和示例here 对于iOS之前的版本8,要做到这一点比较复杂。您应该手动计算单元格的高度,然后在表视图的委托方法中提供此高度(在索引路径处为heighForRow)。希望这会帮助你。
答
它归结为这个
var myRowHeight: CGFloat = 100 //initialize this to some value close to what it will probably be
override func viewDidLoad() {
//control row height for variable text size
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = myRowHeight
}
override func viewWillAppear(animated: Bool) {
//set row height right before view appears so autolayout has time to estimate it
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
}
这是对我工作的解决方案。
在没有任何代码的ios 7中,我们不能增加单元格高度吗? – skkrish 2014-12-05 09:17:30