基于两个子视图高度的自定义TableView单元格 [英] Self-Sizing TableView Cells based on two subview's height

查看:168
本文介绍了基于两个子视图高度的自定义TableView单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TableView说明

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0



单元格描述

我在ContainerView和1 ImageView中有一个包含2个标签(标题,描述)的单元格,如下所示。单元格高度将根据描述标签的内容而有所不同。



ImageView约束





添加视图并将两个标签放入其中。



标签容器约束





标签标题约束





标签说明约束




编辑输出



TableView Description
tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 60.0

Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views

There are two cases that I should handle

  1. ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
  2. ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
    Expected Result in both the cases

Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)

Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?

解决方案

Use these delegates,

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

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

Edit

Try this one.

Your view hierarchy should be like this

ImageView Constraints

Add a view and put both labels into it.

Label Container contraints

Label Title constraint

Label Description constraint

Edit Output

这篇关于基于两个子视图高度的自定义TableView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆