2017-02-23 77 views
0

TableView中描述
tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 60.0自調整大小的TableView細胞基於兩個子視圖的高度

小區描述
我有一個ContainerView 2個內標籤(標題,描述)和低於1個ImageView的一個細胞。單元高度將根據說明標籤的內容而有所不同。
Contraints of all views

有兩種情況,我應該處理

  1. ContainerView.height比(ImageView.height + ImageView.Top + ImageView.Bottom)更大。這裏單元格的高度將基於ContainerView.height
  2. ContainerView高度小於(ImageView.height + ImageView.Top + ImageView.Bottom)。在這裏,我期望Cell應該考慮(ImageView.height + ImageView.Top + ImageView.Bottom)作爲高度並使ContainerView垂直居中Cell。
    Expected Result in both the cases

問題
如果我設置第一種情況的限制,然後第二個情況是不工作的,反之亦然(我知道,通過消除ContrainerView.Top,底部,使其垂直中心的SuperView情況2的結果可以實現)

有沒有一種方法在兩種情況下通過使用相同的IB約束和UITableViewAutomaticDimension實現預期的結果?

+0

如果你在垂直方向居中SuperView,會出現什麼問題? –

+0

使用'heightForRow'和'estimatedheightfor'。它會根據您的容器大小自動調整您的單元格。我認爲這就是你想要的。 –

+0

@agent_stack如果我使用該約束,則_Case 1_將失敗。對於實例,當描述標籤的內容超過3行時,文本將被截斷 – Shyam

回答

0

將固定的高度和寬度賦予圖像視圖。否則,讓tableViewCell知道imageview的頂部和底部。因此,它可以計算出正確的單元格高度

0

使用這些代表,

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 100; // height of default cell 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
} 

編輯

試試這一個。

您的視圖層次應該是這樣的

enter image description here

ImageView的約束

enter image description here

添加視圖,把兩個標籤進去。

標籤集裝箱約束上

enter image description here

標籤名稱約束

enter image description here

標籤說明約束

enter image description here

編輯輸出

enter image description here

+0

是的。我正在那樣做。實際上,我使_Case 1_與這些[約束](https://i.stack.imgur.com/pyZMa.png)一起工作,並且正如我在** Problem **中提到的,我可以添加不同的約束條件並使_Case 2_工作,但我想知道如果我可以添加任何約束,以便這兩種情況都可以工作。 – Shyam

+0

@Shyam檢查編輯 –

+0

@Shyam如果您仍然面臨任何問題,我添加了編輯答案的輸出。希望你現在能夠達到你的要求。如果您仍然有任何疑問,請告訴我。 –

相關問題