【问题标题】:Tableview cell height as per content in it表格视图单元格高度根据其中的内容
【发布时间】:2020-02-25 08:27:35
【问题描述】:

使用 swift5 我尝试了下面的代码,但无法使其成为可调节的单元格高度。任何人都可以帮助我吗?

    tableView.rowHeight = tableView.frame.height/8
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        tableView.estimatedRowHeight = 100
        return UITableView.automaticDimension
    }

现在

我想要什么

查看架构

单元架构

【问题讨论】:

  • 你正在寻找this方法,那是你应该返回tableHeight / 8的地方
  • 如果你正在寻找tableview的动态高度,你可以分配tableView.estimatedRowHeight = 100 // you cell's heighttableView.rowHeight = UITableView.automaticDimension,你也可以把tableview的高度约束写成`self.tblHConstraint.constant = self.tblOfferView。 willDisplay cell 方法中的 contentSize.height`

标签: ios swift


【解决方案1】:

自动高度

您需要确认UITableViewDelegate,然后调用这两个方法。

func tableView(UITableView, heightForRowAt: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

 func tableView(_ tableView: UITableView,  estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

或者

tableview.rowHeight = UITableView.automaticDimension
tableview.estimatedRowHeight = 100

但是在这两种情况下,单元格中的视图都需要同时具有底部和顶部约束。阅读this question 接受的答案以获取更多信息。

更多信息。

UITableViewDelegate

Configuring Cell Height and Layout

【讨论】:

  • 感谢您的回答。我已经尝试了答案,但单元格高度仅由 return 100 更改...并且我添加了我认为添加了约束的单元格架构师已经。我错过了哪里???太棘手了...@mahan
  • @tzxdtc10 我不知道这对你来说是否棘手。你需要自动高度还是固定高度(table.frame.height/8)? print(table.frame.height) 在上述方法之一中,如果您有固定的高度。我认为是0nill
  • @tzxdtc10 我更新了 asnwere。有问题。在heightForRowAt 方法中返回return UITableView.automaticDimension,在estimatedHeightForRowAt 中返回100
  • 感谢您的回答,但它对我不起作用。最后,我将 tableView 更改为 UIView。
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多