[英]UILabel inside UITableViewCell - height isn't adjusting correctly
Check out the second UILabel
inside of the pink UITableViewCell.
在粉红色的UITableViewCell.
出第二个UILabel
UITableViewCell.
As you can see, the text inside it is cut-off. 如您所见,其中的文本是截止的。 I've been trying to get the height of this UILabel
to correctly expand or shrink as a function of how much text is plugged into it - and its not working. 我一直在尝试提高此UILabel
的高度,以根据插入的文本量正确地进行扩展或收缩-并且它不起作用。
• I'm using AutoLayouts
•我正在使用AutoLayouts
• The UILabel
is pinned on all 4 sides to the ContentView
- which of course is inside the UITableViewCell
• UILabel
在所有4侧固定在ContentView
-当然在UITableViewCell
内部
• The Cells are all Static
cells by the way, not Prototype
cells. •单元格全部是Static
单元格,而不是Prototype
单元格。
• Here's the code I'm using: •这是我正在使用的代码:
(in viewWillAppear) (在viewWillAppear中)
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
then, in viewDidAppear (and FYI, I also tried putting the following in will
and didLayoutSubviews
and in all sorts of other permutations:) 然后,在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()
}
Finally, in heightForRowAtIndexPath
I use the global var to adjust the TableViewCell's height: 最后,在heightForRowAtIndexPath
我使用全局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
}
}
The result is mixed: the height of the Label - and the Cell housing it - does adjust - but not enough. 结果好坏参半:标签的高度-以及容纳它的单元的高度- 确实有所调整-但还不够。 It always ends up being too short. 它总是以太短而告终。 And I don't mean that it just cuts off a couple of words, its way too short, cutting off multiple sentences. 我的意思并不是说,它只是切断一对夫妇的话,它太短 ,切断了多个句子。
But again: the height does adjust. 但同样:高度确实会调整。 And that's the weird part. 这就是奇怪的部分。 Its working - just not enough. 它的工作-仅仅是不够的。
Any tips would be greatly appreciated! 任何提示将非常感谢!
You need to tell table view to dynamically scale the cells based on content: 您需要告诉表视图以根据内容动态缩放单元格:
tableView.rowHeight = UITableViewAutomaticDimension
Then, you can remove your implementation of heightForRowAtIndexPath:
- cells should scale automatically. 然后,您可以删除heightForRowAtIndexPath:
的实现heightForRowAtIndexPath:
-单元格应自动缩放。 Remember to setup layout constraints correctly though. 但是请记住要正确设置布局约束。 Without it dynamic sizing won't work. 没有它,动态调整大小将无法工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.