UITableView单元的动态高度问题(Swift) [英] Dynamic Height Issue for UITableView Cells (Swift)

查看:135
本文介绍了UITableView单元的动态高度问题(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可变长度的动态文本被注入tableview单元格标签。为了使tableview单元格的高度动态调整,我在中实现了viewDidLoad()

Dynamic text of variable length are being injected into tableview cell labels. In order for the tableview cells' heights to be dynamically sized, I have implemented in viewDidLoad():

self.tableView.estimatedRowHeight = 88.0
self.tableView.rowHeight = UITableViewAutomaticDimension

这适用于尚未滚动到的单元格(在滚动到单元格时调用 UITableViewAutomaticDimention ),但不适用于单元格最初在使用数据加载表时在屏幕上呈现。

This works nicely for the cells that have yet to be scrolled to (as UITableViewAutomaticDimention is called upon scrolling to the cell), but not for the cells that are initially rendered onscreen upon loading the table with data.

我尝试过简单地重新加载数据(如许多其他资源所示):

I have tried simply reloading the data (as suggested in many other resources):

self.tableView.reloadData()

viewDidAppear() viewWillAppear()中,它无济于事。我迷了..有没有人知道如何让xcode渲染最初在屏幕上加载的单元格的动态高度?

in both viewDidAppear() and viewWillAppear() and it was to no avail. I am lost.. does anyone know how to have xcode render the dynamic height for the cells loaded initially on screen?

如果有更好的替代方法,请告诉我。

Please let me know, if there is a better alternate method.

推荐答案

试试这个:

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

编辑

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Swift 4

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

定义以上两种方法。

它解决了这个问题。

Define above Both Methods.
It solves the problem.

PS:这需要最高和最低限制。

这是示例

这篇关于UITableView单元的动态高度问题(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆