heightForRowAt与estimatedHeightForRowAt的区别是什么? [英] what is the difference heightForRowAt vs estimatedHeightForRowAt?

查看:1634
本文介绍了heightForRowAt与estimatedHeightForRowAt的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我想设置我的行高时,然后设置

Normally when i want set my row height then set

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

受益于 estimatedHeightForRowAt 我应该何时使用它。

what is the benefited with estimatedHeightForRowAt when should i use it.

更新!如果我想设置estimatedHeightForRowAt:

update! if i want to set estimatedHeightForRowAt:

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

        return // what should i put here
    }

更新:

如果我提供了expectHeightForRowAt作为我的愿望,是否有任何效果tableview滚动? 。例如,我给出了估计的HeightForRowAt 500

UPDATE:
Is there any effect tableview scrolling if i provide as my wish estimatedHeightForRowAt? . for example i give in estimatedHeightForRowAt 500

推荐答案

当您拥有一些自定义单元格时,这种差异会变得更有意义它们的高度与大多数其他细胞不同。在这种情况下,操作系统使用 estimatedHeightForRowAt 作为默认值(这会优化您的加载时间),但如果为行指定了不同的高度,那么它将使用它。

This difference becomes more meaningful when you have some custom cells which their height is different from most of your other cells. In that case the OS use estimatedHeightForRowAt as a default (This optimizes your loading time), but then if specified a different height for a row then it would use that.

关于放在那里的内容:

如果没有变化那么你就把300放在一起。基本上你把你的默认/最期望的高度放在那里。对于heightForRowAt,根据您的需要,您可能需要基于交换机在部分/行并更改高度。如果没有任何变化,那么仍然只需要300那里

If there is no variation then you put the same 300. Basically you put your default/most expected height there. For the heightForRowAt depending on your needs you may want to do switch based on section/row and change height. If don't have any variations then still just put 300 there

我不确定这是不是你问的原因或者不是,但如果你问这个是因为你有不同高度的动态tableViewCells,那么解决方案是:

I'm not sure if this is why you asked or not, but if you were asking this because you had dynamic tableViewCells with different heights, then the solution is:


  1. 创建时使用自动布局你的表视图单元格。

  2. 将tableview rowHeight 设置为 UITableViewAutomaticDimension

  3. 设置 estimatedRowHeight 或实现身高估计委托方法。

  1. Use Auto Layout when creating your table view cells.
  2. Set the tableview rowHeight to UITableViewAutomaticDimension.
  3. Set the estimatedRowHeight or implement the height estimation delegate method.

基本上做:

override func viewDidLoad() {  
    super.viewDidLoad()

    self.tableView.estimatedRowHeight = 80 // or any other number that makes sense for your cells
    self.tableView.rowHeight = UITableViewAutomaticDimension
}

欲了解更多信息,请参阅 RW教程和这个高投票问题

For more see this RW Tutorial and this high voted question

这篇关于heightForRowAt与estimatedHeightForRowAt的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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