表视图单元格大小相对于标签大小



我有一个UITableView,并且有自己的自定义单元格,其中包含单元格内的UILabel

我想根据标签中的文本增加表视图行大小。 我正在使用自动布局,我的标签从左边有 20 个填充,从右、上和下有 8 个填充。

我被困在这里是因为标签上下文有时很小,有时非常大,我想在增加文本时增加该行的大小。

UILabel.numberOfLines设置为 0 并将其添加到您的UITableViewDelegate

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

最新更新