title: iOS - heightForRowAtIndexPath does not fire during scrolling
body: <p>I am writing a simple iOS app using Xcode4, which uses a table view to display a list of stories (fetched from a URL). I'm displaying the story titles as UILabels, and as a subview of the table cell.</p>
  
  <p>I am over-riding <code>heightForRowAtIndexPath</code> to calculate the correct height for the cells according to the length of each story title. I'm adding the label to the cell in <code>cellForRowAtIndexPath</code>. When I run the app in the simulator, everything is rendered well. However: when I scroll down and scroll up, the labels get messed up. They get truncated and over-run. I debugged a little, and found that the <code>heightForRowAtIndexPath</code> method is not fired during scrolling, so the cell heights are not re-calculated, and therefore the label text overflows, and gets rendered ugly. Here is the relevant code:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
          [cell autorelease];
      }
  
      /* NOTE: code to load trimmedTitle dynamically is snipped */
      NSString* trimmedTitle;
  
      UIFont *cellFont = [UIFont fontWithName:@"Georgia" size:14.0];
      CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
      CGSize labelSize = [trimmedTitle sizeWithFont:cellFont constrainedToSize:constraintSize 
                                      lineBreakMode:UILineBreakModeWordWrap];
  
      UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 230, labelSize.height)];
      tempLabel.lineBreakMode = UILineBreakModeWordWrap;
      tempLabel.text = trimmedTitle;
      tempLabel.numberOfLines = 0;
  
      [cell.contentView addSubview:tempLabel];
      [tempLabel release];
  
      return cell;
  }
  
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSString* cellText;  // code to load cellText dynamically is snipped off
      UIFont *cellFont = [UIFont fontWithName:@"Georgia" size:14.0];
      CGSize constraintSize = CGSizeMake(230.0f, MAXFLOAT);
      CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
  
      return labelSize.height + 20;
  }
  </code></pre>