2017-02-23 71 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如果您仍然面临任何问题,我添加了编辑答案的输出。希望你现在能够达到你的要求。如果您仍然有任何疑问,请告诉我。 –

相关问题