一些dynamic高度的tableview单元格,其他高度固定的单元格

我有一个tableview。 我想要一些具有dynamic高度的tableview单元格,而其他单元格的高度是固定的。 对于dynamic高度,我在viewDidLoad视图控制器委托中使用了以下几行代码。

tableView.estimatedRowHeight = 200 tableView.rowHeight = UITableViewAutomaticDimension 

在一个tableview中有固定加权单元格和自定义单元格的有效方法是什么?

提前致谢。

Swift 3

您可以通过执行此操作来反转tableview中的dynamic和静态单元格高度

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { return 120.0 // for static cell height } else { return UITableViewAutomaticDimension // for dynamic cell height } } 

你必须实现另一个tableview estimatedHeightForRowAt方法

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { return 120.0 } else { return UITableViewAutomaticDimension } } 

不要忘记将标签lineNumbers设置为0和lineBreakMode哪个标签将dynamic扩展,最重要的是不要设置标签高度固定。

斯威夫特4

您可以使用此函数的indexPath来返回某些行的固定高度和其他行的dynamic高度。

 // UITableViewAutomaticDimension calculates height of label contents/text func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 

假设indexPath.row = 0和1分别包含静态高度和dynamic高度

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.row == 0 { return 120 } else { return UITableViewAutomaticDimension } }