Case
Usually you should use the cellForRowAtIndexPath delegate cellForRowAtIndexPath to customize your cell. The information given for the cell is important for how the cell will be drawn and what size.
Unfortunately, the delegate method heightForRowAtIndexPath is called before the delegate method cellForRowAtIndexPath , so we canโt just tell the delegate to return the cell height, because at that time it will be 0.
Thus, we need to calculate the size before the cell is drawn in the table. Fortunately, there is a method that does just that, sizeWithFont , which belongs to the NSString class. However, there is a problem to dynamically calculate the correct size in order to know how the elements in the cell will be represented. I will do this in an example:
Imagine a UITableViewCell that contains a label named textLabel . As part of the cellForRowAtIndexPath delegate cellForRowAtIndexPath we put textLabel.numberOfLines = 0 , which basically tells the label that it can have as many lines as it needs to present text for a specific width. The problem arises if we give textLabel text that is larger than the width originally set for textLabel. A second row will appear, but the cell height will not be automatically adjusted, and we will get a messy table view.
As mentioned earlier, we can use sizeWithFont to calculate the height, but it needs to know which font is used, for what width, etc. If for reasons of simplicity we just care about the width, we could hard code that the width will be around 320.0 (this does not take into account the gasket). But what happens if we use UITableViewStyleGrouped instead of a simple width, then it will be around 300.0, and the cell will be corrupted again. Or what happens if we move from portrait to landscape, we have much more space, but it will not be used, since we hardcoded 300.0.
This is the case when at some point you should ask yourself the question of how much you can avoid hard coding.
My own thoughts
You can call the cellForRowAtIndexPath method, which belongs to the UITableView class, to get a cell for a specific section and row. I read a couple of posts that said you donโt want to do this, but I donโt understand this. Yes, I agree that it will already select the cell, but the delegate method heightForRowAtIndexPath is called only for the cells that will be visible, so the cell will be selected anyway. If you use dequeueReusableCellWithIdentifier , the cell will not be selected again in the cellForRowAtIndexPath method, a pointer will be used instead, and the properties will just be configured. Then what is the problem?
Note that the cell is NOT drawn inside the cellForRowAtIndexPath delegate cellForRowAtIndexPath , when the table view cell becomes visible, the script calls the setNeedDisplay method in UITableVieCell, which invokes the drawRect method to draw the cellForRowAtIndexPath , calling the cellForRowAtIndexPath delegate cellForRowAtIndexPath not directly lose performance because it needs to be done twice.
So, by calling the delegate method cellForRowAtIndexPath in the delegate method heightForRowAtIndexPath , we get all the necessary information about the cell to determine its size.
Perhaps you can create your own sizeForCell method that goes through all the parameters, what if the cell is in the style of Value1 or Value2, etc.
Conclusion / Question
This is just a theory that I described in my thoughts, I would like to know if I wrote correctly. Or maybe there is another way to do the same. Please note that I want to be able to make everything as flexible as possible.