3 回答

TA貢獻1775條經驗 獲得超11個贊
通過xib或storyboard創建您的單元格。給出它的出口內容?,F在在CellForRowAtIndexPath中調用它。例如。如果要根據Comment的標簽文本設置單元格高度。 在此輸入圖像描述
所以設置你的commentsLbl.numberOfLine = 0;
所以設置你的commentsLbl.numberOfLine = 0;
然后在ViewDidLoad中
self.table.estimatedRowHeight = 44.0 ;
self.table.rowHeight = UITableViewAutomaticDimension;
現在
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

TA貢獻1784條經驗 獲得超2個贊
這里的許多答案都不尊重桌子的變化或太復雜。使用autolayout時,使用UITableView正確設置的子類intrinsicContentSize是一個更容易的解決方案。不需要高度限制等。
class UIDynamicTableView: UITableView
{
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: self.contentSize.height)
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
UIDynamicTableView在接口構建器中設置TableView的類并觀察魔法,因為此TableView將在調用后更改其大小reloadData()。
- 3 回答
- 0 關注
- 716 瀏覽
添加回答
舉報