所以基本上,我想嵌入一个 UITableView
里面UITableViewCell
.
我用过UIStackView
使用其 AutoLayout
力量!
无论如何,“Inner UITableView”的每一行都将包含一个固定大小的 UIImageView
和一个 UILabel
的lines = 0
(即不固定)
UIStackView
(水平)-> UIImageView
+ UILabel
Requirements:
- 由于服务器提供随机文本,InnerTableView 的每行/单元格的大小应该是动态的。从而使得整个内在
UITableView
的高度为automaticDimension
UILabel
的文本以上内UITableView
也是动态的。 (因此首先使用UIStackView
是有意义的)
注意:主要 UITableView
由多个不同类型的单元组成(这基本上是一个 BotChatBot)
另外:estimatedHeight
可以在40点左右。
Problem:
InnerTableView 的大小是动态调整的,导致其 cellForRowAt
方法不会被调用一次,因此不允许 UIStackView
由于 contentSize
没有变化而增加其长度。
为了解决上述问题,我尝试了:
- 将 UITableVIew 的初始高度限制设置为 10(某个随机数),
estimatedRowHight
对于 InnerTableView 为 50,因此将 UITableView 的高度设置为tableViewInsideCellHeightConstraint?.constant = tableViewInsideCell.contentSize.height
这在某种程度上给了我一些空间,但当行的高度超过估计高度 40 时它仍然不可靠
附注layoutIfNeeded()
被触发为 TableView
, StackView
和ParentCell
I also have another way of solving the above problem:
- 使用估计高度,将常数(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 出现明显的抖动:[
Question:
针对上述问题,有什么具体的解决办法吗? 我尝试一遍又一遍地搜索它......无法真正找到任何强有力的修复!
最佳答案
当你调用api获取subTableView中的数据时,你可以尝试如下:
self.tvCell?.table.reloadData() //tv cell is my Parent TableViewCell
self.table.beginUpdates() //Parent TableView
self.table.endUpdates() // Parent TableView
它对我来说运行良好,希望对您有所帮助。
关于ios - UITableView里面UITableViewCell高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56125813/