Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieve custom prototype cell height from storyboard?
    primarykey
    data
    text
    <p>When using "Dynamic Prototypes" for specifying <code>UITableView</code> content on the storyboard, there is a "Row Height" property that can be set to Custom.</p> <p>When instantiating cells, this custom row height is not taken into account. This makes sense, since which prototype cell I use is decided by my application code at the time when the cell is to be instantiated. To instantiate all cells when calculating layout would introduce a performance penalty, so I understand why that cannot be done.</p> <p>The question then, can I somehow retrieve the height given a cell reuse identifier, e.g. </p> <pre><code>[myTableView heightForCellWithReuseIdentifier:@"MyCellPrototype"]; </code></pre> <p>or something along that line? Or do I have to duplicate the explicit row heights in my application code, with the maintenance burden that follows?</p> <p><strong>Solved, with the help of @TimothyMoose:</strong></p> <p>The heights are stored in the cells themselves, which means the only way of getting the heights is to instantiate the prototypes. One way of doing this is to pre-dequeue the cells outside of the normal cell callback method. Here is my small POC, which works:</p> <pre><code>#import "ViewController.h" @interface ViewController () { NSDictionary* heights; } @end @implementation ViewController - (NSString*) _reusableIdentifierForIndexPath:(NSIndexPath *)indexPath { return [NSString stringWithFormat:@"C%d", indexPath.row]; } - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(!heights) { NSMutableDictionary* hts = [NSMutableDictionary dictionary]; for(NSString* reusableIdentifier in [NSArray arrayWithObjects:@"C0", @"C1", @"C2", nil]) { CGFloat height = [[tableView dequeueReusableCellWithIdentifier:reusableIdentifier] bounds].size.height; hts[reusableIdentifier] = [NSNumber numberWithFloat:height]; } heights = [hts copy]; } NSString* prototype = [self _reusableIdentifierForIndexPath:indexPath]; return [heights[prototype] floatValue]; } - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString* prototype = [self _reusableIdentifierForIndexPath:indexPath]; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:prototype]; return cell; } @end </code></pre>
    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