5

Normally when i want set my row height then set

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

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

update! if i want to set estimatedHeightForRowAt:

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

        return // what should i put here
    }

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

cristainlika3
  • 279
  • 1
  • 4
  • 15

2 Answers2

5

As specified in the iOS documentation:

If the table contains variable height rows, it might be expensive to calculate all their heights and so lead to a longer load time. Using estimation allows you to defer some of the cost of geometry calculation from load time to scrolling time.

Andrea Gottardo
  • 1,329
  • 11
  • 23
  • can you share your own language – cristainlika3 Jul 23 '17 at 16:52
  • This might give you some context: https://stackoverflow.com/questions/40341652/estimatedheightforrowat-is-not-called-on-scroll Basically, it's just a design decision to improve performance. Unless you have thousands of cells, I don't think it can make a difference. What are you trying to implement? – Andrea Gottardo Jul 23 '17 at 16:55
  • Is the height of your cells always the same? If this is the case, then you will have the same value as in `heightForRowAt`. – Andrea Gottardo Jul 23 '17 at 16:58
  • if not what should i do – cristainlika3 Jul 23 '17 at 16:59
  • Let's see if this is a good explanation. You need to provide a simple approximation. Imagine an app like Instagram. The `UITableView` gets populated with images of various heights. The thing is that we don't know the real height of each image until the download has been completed. But we still have to provide a height for the cells! So in the meantime we just provide an approximation (maybe, the average image height on Instagram). The cell will eventually get redrawn using the value provided by `heightForRowAt` (that's the real image height). If it's still unclear, let me know. – Andrea Gottardo Jul 23 '17 at 17:05
  • is there any effect tableview scrolling if `estimatedHeightForRowAt` is wrong . for example i give in estimatedHeightForRowAt 500 . – cristainlika3 Jul 23 '17 at 17:09
2

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.

As for what to put there:

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


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. 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.

Basically do:

override func viewDidLoad() {  
    super.viewDidLoad()

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

For more see this RW Tutorial and this high voted question


NOTE

If you don’t give the estimatedRowHeight then tableview will dequeue a lot more cells, because it’s trying to be smart and needs to prepare itself for smooth scrolling. It will trigger both your cellForRow & willDisplay callbacks

If you give estimatedRowHeight a value, then it won’t dequeue cells offscreen anymore

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • @cristainlika3 If there 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 – mfaani Jul 23 '17 at 17:18
  • what do you mean by section/row and change height ? – cristainlika3 Jul 23 '17 at 17:19
  • how can i put section height what do you mean by section change height ? – cristainlika3 Jul 23 '17 at 17:21
  • @cristainlika3 you can change sectionHeight using a different method. Using this method you can change height of a row using switches. See the last code snippet of [this answer](https://stackoverflow.com/a/35945961/5175709) – mfaani Jul 23 '17 at 17:25
  • is there any effect tableview scrolling if `estimatedHeightForRowAt` is wrong . for example i give in `estimatedHeightForRowAt` 500 – cristainlika3 Jul 23 '17 at 17:29
  • @cristainlika3 Yes. It's not substantial, but the more you make quick changes in your tableView and scroll data...the more it becomes important. To be honest I wouldn't spend too much time on this. Just do the right thing. Set your best educated guess in the estimate and then make whatever necessary changes in the `heightForRowAt` – mfaani Jul 23 '17 at 17:31
  • is it possible to `estimatedHeightForRowAt` less than `heightForRowAt`? – cristainlika3 Jul 23 '17 at 17:38
  • @cristainlika3 I've answered that. Yes! + you can try yourself. – mfaani Jul 23 '17 at 17:40