【发布时间】:2016-06-23 02:16:10
【问题描述】:
查看粉红色UITableViewCell. 中的第二个UILabel 如您所见,其中的文本被截断。我一直在尝试让 UILabel 的高度根据插入的文本数量正确扩展或缩小 - 并且它不起作用。
• 我正在使用AutoLayouts
• UILabel 的所有 4 个侧面都固定在 ContentView 上 - 当然是在 UITableViewCell 内部
• 顺便说一下,这些细胞都是Static 细胞,而不是Prototype 细胞。
• 这是我正在使用的代码:
(在视图中将出现)
descriptionLabel.text = " < a bunch of text goes here > "
descriptionLabelSize = descriptionLabel.sizeThatFits(descriptionLabel.bounds.size)
// here's a global var I use to store the height:
descriptionLabelHeight = descriptionLabelSize.height
然后,在 viewDidAppear 中(仅供参考,我还尝试将以下内容放入 will 和 didLayoutSubviews 以及各种其他排列中:)
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var newFrame = descriptionLabel.frame
newFrame.size.height = descriptionLabelHeight
descriptionLabel.frame = newFrame
descriptionLabel.setNeedsLayout()
view.layoutIfNeeded()
}
最后,在heightForRowAtIndexPath我使用全局变量来调整TableViewCell的高度:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch indexPath.row {
case 0: return 50
case 1: return descriptionLabelHeight+20 // the + 20 is for padding & margin
default: return 50
}
}
结果好坏参半:标签的高度 - 以及容纳它的单元格 - 确实调整 - 但还不够。它总是以太短而告终。我并不是说它只是截断了几个词,它的方式太短了,截断了多个句子。
但同样:高度会调整。这就是奇怪的部分。它的工作 - 还不够。
任何提示将不胜感激!
【问题讨论】:
标签: ios uitableview uilabel height