[英]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.