Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic UITableViewCell height for complex cells
    text
    copied!<p>I'm trying to calculate dynamic height of cell in my UITableView.</p> <p>My cells are very complex : it could be a dozen of label, uitextfield, buttons etc. In fact, it's like a form in a cell (with de UITableViewGroupedStyle, it really looks like a form !). So I don't care about "sizeWithFont:withConstrainedSize: etc..."</p> <p>I have a custom class method in my cell which calculate the height of the cell (from the height of components inside).</p> <p>Now in my UITableViewController, i want to set the correct height for my cells. To do this i need to call getHeight on my cell. But... If i call "cellForRowAtIndexPath" in my "heightForRowAtIndexPath", it loops (because cellForRow... needs heightForRow.... to build a cell).</p> <p>I also tried to build the cell in my cellForRowAtIndexPath, get its height and store it in an Array to retrieve it in heigtForRowAtIndexPath. Because "heightForRowAtIndexPath" is called BEFORE "cellForRow...", the first time I return a default value (CGFLOAT_MAX). When my cell is built, i return its height retrieved from my array. But to call it the second time, i need to call it manually, so when my cell is built in "CellForRow..." i call reloadData. But... (again and again) it doesn't work anyway... In fact, sometimes it works (with UITableViewGroupedStyle) ans sometimes no (withUITableViewPlainStyle,beacause it dosent like the CGFLOAT_MAX default value...)</p> <p>It makes me crazy !</p> <p>This version works for UITableViewGroupedStyle :</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MFFormListCell *cell = [self.cellSizeMap objectForKey:indexPath]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { MFFormListCell *cell = [self.cellSizeMap objectForKey:indexPath]; if(!cell || ![cell isEqual:[NSNull null]]) { [self.cellSizeMap setObject:[NSNull null] forKey:indexPath]; cell = [tableView dequeueReusableCellWithIdentifier:@"FormListCell" forIndexPath:indexPath]; [self.cellSizeMap setObject:cell forKey:indexPath]; } if([cell isEqual:[NSNull null]]) return CGFLOAT_MAX; //Valeur max pour que le système prennent en compte toutes les cellules à afficher else return [[cell getHeight] floatValue]; } </code></pre> <p>Don't ask about first "if" in "heightForRowAtIndexPath:", it's a bad trick to avoiding loop.</p>
 

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