2014-12-05 113 views
0

我一直在從堆棧溢出中搜索很久,發現了一些相關的問題,但並不完美。 有人可以解釋詳細說明什麼是使標籤增長的高度基於tableview單元格中的文本。我想增加標籤高度,然後單元格高度。 謝謝。動態單元格高度的自動佈局約束條件

回答

1

對於iOS 8+,您可以找到解釋和示例here 對於iOS之前的版本8,要做到這一點比較複雜。您應該手動計算單元格的高度,然後在表視圖的委託方法中提供此高度(在索引路徑處爲heighForRow)。希望這會幫助你。

+0

在沒有任何代碼的ios 7中,我們不能增加單元格高度嗎? – skkrish 2014-12-05 09:17:30

0

它歸結爲這個

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 
    } 

這是對我工作的解決方案。