简体   繁体   English

使用tableViews自定义单元格时如何在运行时更改表视图单元格的高度?

[英]How to change table view cell height at runtime while using tableViews self sizing cells?

I have main tableView M which has X number of cells, each x has sub tableView S with N number of cells. 我主要的tableView 中号 ,其具有细胞的X数量,每个x具有子的tableView S采用细胞的N个。 I use estimatedRowHeight and UITableViewAutomaticDimension to create dynamic cells height . 我使用EstimatedRowHeightUITableViewAutomaticDimension创建动态单元格height The question is, how to force the M table view to relayout and change the height of x cells to fit the content size of the S tableView at runtime? 问题是,如何在运行时强制M表视图重新布局并更改x单元格的高度以适合S tableView的内容大小?

here is an image of what i want to implement 这是我要实现的图像

I think you need to manually calculate the height of each cell(each X cell) in table view M. First find the height of your tableView S's height in each X cell. 我认为您需要手动计算表格视图M中每个单元格(每个X单元格)的高度。首先找到每个X单元格中tableView S的高度。

Calculating TableView S's height: 计算TableView S的高度:

Height = Number of cells * Height of each cell(N) 高度=单元数*每个单元的高度(N)

X cell's height = Height X单元格的高度=高度

NOTE: Here you'd mentioned that you are using UITableViewAutomaticDimension for tableView S also. 注意:在这里,您提到您还使用UITableViewAutomaticDimension作为tableViewS。 So you ned to find the height of the each cell N, based on your content in it. 因此,您需要根据其中的内容查找每个单元格N的高度。 E,g: If you are gonna to load string in cell N, just calculate label height based on string length and then provide your Cell N height by adding proper spacing height with label height. 例如,如果您要在单元格N中加载字符串,只需根据字符串长度计算标签高度,然后通过在标签高度上添加适当的间距高度来提供单元格N的高度。

Thanks. 谢谢。

You have to calculate it manually height of each cell in table view M. before that find out the height of your tableView S's height with N cells. 您必须手动计算表格视图M中每个单元格的高度,然后才能找到N个单元格的tableView S高度。

  1. get S's tableview.contentSize.height 获取S的tableview.contentSize.height
  2. give this contentSize to the M's cell X. 将此contentSize赋予M的单元格X。
  3. now get M's tableview.contentSize.height 现在得到M的tableview.contentSize.height

make two individual methods for getting tableview's height by their cells and write them in "cellAtRow" delegate of tableview. 制作两个单独的方法来通过其单元格获取表格视图的高度,并将它们写入表格视图的“ cellAtRow”委托中。 (make iboutlet of height constraint of auto layout). (使iboutlet具有自动布局的高度限制)。 just reload table when it need to change at run time. 只需在需要在运行时更改表时重新加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM