【问题标题】:Swift dynamic table cell height [duplicate]Swift动态表格单元格高度[重复]
【发布时间】:2016-08-03 02:41:13
【问题描述】:

我有带有原型单元格的表格视图, 在单元格中,我有 imageview 和一些文本。 文本标签是原型单元格中的一行,但有时不止一行,我在服务器调用后将数据加载到表视图中。 故事板 Lines 中的标签设置为 0,换行符设置为 Word Wrap。 我也试过http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/

但没有效果。 如果我使用Using Auto Layout in UITableView for dynamic cell layouts & variable row heights 一切正常,因为标签文本是预定义的,但我正在从服务器加载数据,并在 API 调用后重新加载 tableView,然后标签只是一行。

【问题讨论】:

  • 您的标签是否对 tableViewCell 有底部约束?
  • 是的,我使用了建议的约束,我应该删除它吗?
  • 它不是重复的,因为如果我有数据,这可以工作,但是我在创建 tableview 之后加载数据,然后调用 reloadData()
  • 不,不要删除它...尝试按照@Scriptable建议的链接的答案

标签: ios uitableview height ios-autolayout


【解决方案1】:

您应该使用UITableViewAutomaticDimension 提供了一种显示动态内容的解决方案。

viewDidLoad中使用以下代码:

tableView.estimatedRowHeight = YourTableViewCellHeight
tableView.rowHeight = UITableView.automaticDimension

Read more from here

希望对您有所帮助。 :)

【讨论】:

【解决方案2】:

确保 UILabel 高度约束不是恒定的,如果恒定则必须从属性检查器设置 Relation 大于或等于

确保根据您的设置 UITableViewestimate 行高。

   tableView.estimatedRowHeight = 85

写在UITableViewCell

 cell.lbl_name.sizeToFit()
 cell.lbl_name.numberOfLines = 0

确保您的UITableViewDelegate 方法

func tableView(tableView: UITableView,
    heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2015-03-25
    • 2012-08-01
    相关资源
    最近更新 更多