3 回答

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

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
這里的許多答案都不尊重桌子的變化或太復(fù)雜。使用autolayout時(shí),使用UITableView正確設(shè)置的子類intrinsicContentSize是一個(gè)更容易的解決方案。不需要高度限制等。
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在接口構(gòu)建器中設(shè)置TableView的類并觀察魔法,因?yàn)榇薚ableView將在調(diào)用后更改其大小reloadData()。
- 3 回答
- 0 關(guān)注
- 736 瀏覽
添加回答
舉報(bào)