[英]Self-Sizing TableView Cells based on two subview's height
TableView說明
tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 60.0
單元格說明
我有一個容器,其中ContainerView和1 ImageView帶有2個Labels(標題,描述),如下所示。 單元格高度將根據“描述標簽”的內容而變化。
所有視圖的約束
我應該處理兩種情況
問題
如果我為第一種情況設置了約束,則第二種情況不起作用,反之亦然(我知道,通過刪除ContrainerView.Top,Bottom並將其垂直居中放置到SuperView情況2中,可以實現)
通過使用相同的IB約束集和UITableViewAutomaticDimension,是否可以在兩種情況下均獲得預期結果?
給圖像視圖指定固定的高度和寬度。 否則,讓tableViewCell知道imageview的頂部和底部。 這樣就可以計算出正確的像元高度
使用這些代表,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
編輯
試試這個。
您的視圖層次結構應如下所示
ImageView約束
添加一個視圖,並在其中放置兩個標簽。
標簽容器約束
標簽標題約束
標簽描述約束
編輯輸出
首先,請確保您使用的是自動調整大小的單元格: https : //developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
使圖像視圖和容器視圖的頂部和底部約束都位於單元格的邊緣,並使其>=
。
或者,您可以嘗試使用“水平堆棧視圖”,並對單元格的每個邊緣進行嚴格的(最高優先級)約束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.