2016-06-21 52 views
-1

我有一個UITableView並在單元格內有我自己的自定義單元格,其中包含UILabelTableView單元格大小與標籤大小有關

我想根據標籤中的文字增加tableView的行大小。 我使用的AutoLayout和我的標籤有20填充左側和8填充右,頂部和底部。

我被困在這裏,因爲標籤上下文有時候很小,有時非常大,我想增加文本增加時該行的大小。

+0

分享一些代碼可以幫助 – ppaulojr

+0

你想在我看來,文本字體大小,但該行高度的變化,其可能的,但不是很好'UI' – meda

回答

1

設置UILabel.numberOfLines爲0,並添加到您的UITableViewDelegate

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 
+0

謝謝你,先生.. !!它的工作原理就是我想要的。 :)) –

+0

根據[文檔](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html),這並不完全正確:首先,您可以在表格視圖中設置這些值,而不是從這些方法中返回它們。其次,您應該使用'estimatedRowHeight'值的實際大小,並且僅對實際的'rowHeight'使用'UITableViewAutomaticDimension'。 –

相關問題