2016-06-21 68 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'。 –