[英]UILabel inside UITableViewCell - height isn't adjusting correctly
在粉紅色的UITableViewCell.
出第二個UILabel
UITableViewCell.
如您所見,其中的文本是截止的。 我一直在嘗試提高此UILabel
的高度,以根據插入的文本量正確地進行擴展或收縮-並且它不起作用。
•我正在使用AutoLayouts
• UILabel
在所有4側固定在ContentView
-當然在UITableViewCell
內部
•單元格全部是Static
單元格,而不是Prototype
單元格。
•這是我正在使用的代碼:
(在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
然后,在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
我使用全局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
}
}
結果好壞參半:標簽的高度-以及容納它的單元的高度- 確實有所調整-但還不夠。 它總是以太短而告終。 我的意思並不是說,它只是切斷一對夫婦的話,它太短 ,切斷了多個句子。
但同樣:高度確實會調整。 這就是奇怪的部分。 它的工作-僅僅是不夠的。
任何提示將非常感謝!
您需要告訴表視圖以根據內容動態縮放單元格:
tableView.rowHeight = UITableViewAutomaticDimension
然后,您可以刪除heightForRowAtIndexPath:
的實現heightForRowAtIndexPath:
-單元格應自動縮放。 但是請記住要正確設置布局約束。 沒有它,動態調整大小將無法工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.