动态更改tableView单元格高度

问题描述:

是否有任何方法来增加或减少表视图中单元格的高度取决于其中的标签文本?动态更改tableView单元格高度

+0

是有是... –

+0

[在UITableView中使用动态单元格布局和可变行高度中的自动布局](http://*.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-小区布局-可变行高度) – mgyky

在这里,您可以根据您的文本的长度获取标签(动态)的高度:

// Set space for content height 
let frameForPostContentLabel = NSString(string: content).boundingRect(with: CGSize(width: view.frame.width - 32, height: 120), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 15)], context: nil) 

,然后你可以设置的tableview的高度:

cell.postContentLabelHeight.constant = frameForPostContentLabel.height