首页 > 解决方案 > UITableView 里面的 UITableViewCell 高度问题

问题描述

查看层次结构

所以基本上,我想嵌入一个UITableViewinside UITableViewCell

我曾经UIStackView使用它的AutoLayout力量!

无论如何,“Inner UITableView”的每一行都将包含一个固定大小UIImageView和一个UILabellines = 0即不固定)

UIStackView(水平)-> UIImageView+UILabel

要求:

注意:MainUITableView由多个不同类型的单元格组成(这基本上是一个 BotChatBot)

另外:estimatedHeight可以在40分左右。

问题:

动态调整大小的 InnerTableView 导致它的cellForRowAt方法不会被调用一次,因此不允许UIStackView增加它的长度,因为contentSize.

为了解决上述问题,我尝试了:

问题

PSlayoutIfNeeded()被触发TableViewStackView并且ParentCell

我还有另一种解决上述问题的方法:

  1. 使用估计的高度,将常量(estimatedHeight)乘以要显示的行数,即列表大小
  2. heightConstraint用计算出的换成Inner UITableView
  3. 每次迭代,计算 UITableView 的总“必需”高度(使用+=visibleCells[i].frame.height)(因为每次迭代将提供单元格的大小(内容大小)
  4. 更新高度约束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 中出现明显的抖动:[

问题:

上述问题有具体的解决方案吗?我一遍又一遍地尝试搜索它..真的找不到任何强有力的修复!

标签: iosswiftuitableviewuitableviewautomaticdimension

解决方案


当您在subTableView中为数据调用 api时,您可以尝试如下:

self.tvCell?.table.reloadData() //tv cell is my Parent TableViewCell
self.table.beginUpdates() //Parent TableView
self.table.endUpdates() // Parent TableView

它对我来说很好,我希望它会对你有所帮助。


推荐阅读