Note that there are some explanatory texts on larger screens.

plurals
  1. POUITableView flexible/dynamic heightForRowAtIndexPath
    primarykey
    data
    text
    <h2>Case</h2> <p>Normally you would use the <code>cellForRowAtIndexPath</code> delegate method to setup your cell. The information set for the cell is important for how the cell is drawn and what the size will be.</p> <p>Unfortunatly the <code>heightForRowAtIndexPath</code> delegate method is called before the <code>cellForRowAtIndexPath</code> delegate method so we can't simply tell the delegate to return the height of the cell, since this will be zero at that time.</p> <p>So we need to calculate the size before the cell is drawn in the table. Luckily there is a method that does just that, <code>sizeWithFont</code>, which belongs to the NSString class. However there is problem, in order to calculate the correct size dynamically it needs to know how the elements in the cell will be presented. I will make this clear in an example:</p> <p>Imagine a <code>UITableViewCell</code>, which contains a label named <code>textLabel</code>. Within the <code>cellForRowAtIndexPath</code> delegate method we place <code>textLabel.numberOfLines = 0</code>, which basically tells the label it can have as many lines as it needs to present the text for a specific width. The problem occurs if we give textLabel a text larger then the width originally given to textLabel. The second line will appear, but the height of the cell will not be automatically adjusted and so we get a messed up looking table view.</p> <p>As said earlier, we can use <code>sizeWithFont</code> to calculate the height, but it needs to know which Font is used, for what width, etc. If, for simplicity reasons, we just care about the width, we could hardcode that the width would be around 320.0 (not taking padding in consideration). But what would happen if we used UITableViewStyleGrouped instead of plain the width would then be around 300.0 and the cell would again be messed up. Or what happends if we swap from portrait to landscape, we have much more space, yet it won't be used since we hardcoded 300.0.</p> <p>This is the case in which at some point you have to ask yourself the question how much can you avoid hardcoding.</p> <h2>My Own Thoughts</h2> <p>You could call the <code>cellForRowAtIndexPath</code> method that belongs to the UITableView class to get the cell for a certain section and row. I read a couple of posts that said you don't want to do that, but I don't really understand that. Yes, I agree it will already allocate the cell, but the <code>heightForRowAtIndexPath</code> delegate method is only called for the cells that will be visible so the cell will be allocated anyway. If you properly use the <code>dequeueReusableCellWithIdentifier</code> the cell will not be allocated again in the <code>cellForRowAtIndexPath</code> method, instead a pointer is used and the properties are just adjusted. Then what's the problem? </p> <p>Note that the cell is NOT drawn within the <code>cellForRowAtIndexPath</code> delegate method, when the table view cell becomes visible the script will call the <code>setNeedDisplay</code> method on the UITableVieCell which triggers the <code>drawRect</code> method to draw the cell. So calling the <code>cellForRowAtIndexPath</code> delegate directly will not lose performance because it needs to be drawn twice.</p> <p>Okay so by calling the <code>cellForRowAtIndexPath</code> delegate method within the <code>heightForRowAtIndexPath</code> delegate method we receive all the information we need about the cell to determine it's size. </p> <p>Perhaps you can create your own <code>sizeForCell</code> method that runs through all the options, what if the cell is in Value1 style, or Value2, etc.</p> <h2>Conclusion/Question</h2> <p>It's just a theory I described in my thoughts, I would like to know if what I wrote is correct. Or that maybe there is another way to accomplish the same thing. Note that I want to be able to do things as flexible as possible.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload