ios - 表视图单元格高度与内部表视图
问题描述
我有一个 tableView,其中每个单元格都包含另一个具有动态行高的 tableView。我的问题是如何设置第一个表的行高以适应内部 tableView 的高度?我正在使用此代码,但它不能正常工作。
var heights: [CGFloat] = []
var loades: [Bool] = []
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.heights[indexPath.row]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath) as! EventCell
cell.date.text = ev.date
cell.delegate = self
cell.event = ev
self.heights[indexPath.row] = cell.tv.frame.height
if self.loades[indexPath.row] == false{
self.loades[indexPath.row] = true
tableView.reloadRows(at: [indexPath], with: .none)
}
return cell}
解决方案
我希望你可以访问你的tableview.contentSize.height
,如果你的tableview是动态的,contentSize获取所有tableview内容的值,这个高度的内容你可以除以你拥有的行数。这只是一个简单的解决方案,有些像这样:
let size = tableView.contentSize.height
let cellSize = size / models.count
其中 models 是在 tableview 内的 tableviewcell 中打印的对象数组
推荐:将单元格内的高度约束放到tableView中,并尝试使用de sizeCell sum刷新
cell.tableViewInsideCell.reloadData()
cell.heightConstraint.constant = tableView.contentSize.height
并设置
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
推荐阅读
- javascript - 如何使用云功能删除 Firestore 中的文档
- google-bigquery - 将 BigQuery DDL 中的变量与外部 GCS 存储桶一起使用
- python - Python线程 - 如何等待来自多个发件人的数据合并结果?
- r - 为什么 TSstudio 包中的 ts_plot 在 RStudios 中显示空白图,但在 Jupyter Notebook 中很好
- typescript - 为组合函数的参数赋予自定义名称
- javascript - 将对象作为函数的参数传递时出现问题
- javascript - Mysql + php数据表未显示特定行
- for-loop - 使用 Twig 生成 Markdown,如何在 twig 中缩进块?
- git - 尝试使用 Visual Studio 2019 社区版创建拉取请求时出现错误:“TF200016”
- c# - c# 中的哪种类型最像 R 中的表达式,为什么?