ios - UITableView 里面的 UITableViewCell 高度问题
问题描述
所以基本上,我想嵌入一个UITableView
inside UITableViewCell
。
我曾经UIStackView
使用它的AutoLayout
力量!
无论如何,“Inner UITableView”的每一行都将包含一个固定大小UIImageView
和一个UILabel
(lines = 0
即不固定)
UIStackView
(水平)-> UIImageView
+UILabel
要求:
- 由于服务器提供的随机文本,InnerTableView 的每一行/单元格的大小应该是动态的。从而使整个 Inner
UITableView
的高度为automaticDimension
UILabel
上面 Inner的文本UITableView
也是动态的。(因此首先使用是有意义UIStackView
的)
注意:MainUITableView
由多个不同类型的单元格组成(这基本上是一个 BotChatBot)
另外:estimatedHeight
可以在40分左右。
问题:
动态调整大小的 InnerTableView 导致它的cellForRowAt
方法不会被调用一次,因此不允许UIStackView
增加它的长度,因为contentSize
.
为了解决上述问题,我尝试了:
- 将 UITableVIew 的初始高度约束设置为 10(某个随机数),
estimatedRowHight
对于 InnerTableView 为 50,从而设置 UITableView 的高度,这以某种方式给了我一些空间,但是当一行的高度超过估计的 40 高度tableViewInsideCellHeightConstraint?.constant = tableViewInsideCell.contentSize.height
时,它仍然不可靠
PSlayoutIfNeeded()
被触发TableView
,StackView
并且ParentCell
我还有另一种解决上述问题的方法:
- 使用估计的高度,将常量(estimatedHeight)乘以要显示的行数,即列表大小
heightConstraint
用计算出的换成Inner UITableView
,- 每次迭代,计算 UITableView 的总“必需”高度(使用
+=visibleCells[i].frame.height
)(因为每次迭代将提供单元格的大小(内容大小) - 更新高度约束
Inner UITableView
if heightForTableView == nil {
if indexPath.row == listDict!.count - 1 {
var heightOfTableView: CGFloat = 0.0
let cells = tableView.visibleCells
for visibleCells in cells {
heightOfTableView += visibleCells.frame.height
}
let newHeight = heightOfTableView + cell.frame.height
reloadMe(newHeight)
}
}
上述修复导致 UI 中出现明显的抖动:[
问题:
上述问题有具体的解决方案吗?我一遍又一遍地尝试搜索它..真的找不到任何强有力的修复!
解决方案
当您在subTableView中为数据调用 api时,您可以尝试如下:
self.tvCell?.table.reloadData() //tv cell is my Parent TableViewCell
self.table.beginUpdates() //Parent TableView
self.table.endUpdates() // Parent TableView
它对我来说很好,我希望它会对你有所帮助。
推荐阅读
- php - 在 AuthenticationService 中使用 TypoScriptFrontendController 功能?/ 将数据保存到身份验证器中的用户?
- java - 使用流 api 聚合列表
- javascript - 循环遍历对象并返回特定属性的值
- python - Pandas - 循环设置WithCopyWarning
- javascript - 如何使用 ffmpeg 创建多质量视频流系统?
- powershell - Powershell AzureAD 脚本将组从静态更改为动态
- python-3.x - CDK 如何在使用 from_role_arn 从现有角色创建角色时添加承担角色
- r - 在 R 中绘制主题建模的时间序列
- javascript - 检查对象数组是否包含与其他对象数组中的对象具有多个值相同的对象
- javascript - 使用 React Hooks 自动调整 iframe 的高度