title: Resize UILabel to fit text inside custom UITableViewCell regardless of width
body: <p>I'm trying to get a label in a cell to be the right size, regardless of device or orientation. I am able to get the row height to size correctly. I am also able to set the label height correctly in <code>cellForRowAtIndexPath</code>, and can check that in my logs. But, by the time it gets to <code>willDisplayRowAtIndexPath</code>, the label height has changed, but only when the cell is not 320 pts wide.</p>
  
  <p>Here's my code-</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  static NSString *CellIdentifier = @"CustomCellIdentifier";
  CustomInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil){
      cell = [[CustomInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
      NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"CustomInfoCell" owner:self options:nil];
      cell = objects[0];
  }
  
  // Configure the cell...
  cell.customTitleLabel.text = [_data[indexPath.row] objectForKey:t];
  
  CGFloat labelWidth = self.view.frame.size.width-40;
  NSLog(@"labelWidth:%f",labelWidth);
  
  NSString *text = [_data[indexPath.row] objectForKey:d];//correct text
  CGSize labelsize=[text sizeWithFont:cell.customDetailLabel.font constrainedToSize:CGSizeMake(labelWidth, 2000.0) lineBreakMode:cell.customDetailLabel.lineBreakMode];
  NSLog(@"labelsize:%f,%f",labelsize.width,labelsize.height);
  
  //For testing
  cell.customDetailLabel.backgroundColor = [UIColor redColor];
  NSLog(@"Pre: %@",cell.customDetailLabel);
  cell.customDetailLabel.frame=CGRectMake(20, 22, labelWidth, labelsize.height);
  
  cell.customDetailLabel.text = text;
      NSLog(@"Post: %@",cell.customDetailLabel);
  return cell;
  }
  </code></pre>
  
  <p>In <code>willDisplayRowAtIndexPath</code> I also print the label info. Here's what one row prints-</p>
  
  <pre><code>2013-03-24 18:33:44.009 Bridge Alert[57793:1e503] labelWidth:728.000000
  2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] labelsize:713.000000,76.000000
  2013-03-24 18:33:44.010 Bridge Alert[57793:1e503] Pre: &lt;UILabel: 0xad3eaf0; frame = (20 20; 280 21); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x17372eb0&gt;&gt;
  2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Post: &lt;UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'Detail'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x17372eb0&gt;&gt;
  2013-03-24 18:33:44.011 Bridge Alert[57793:1e503] Text set: &lt;UILabel: 0xad3eaf0; frame = (20 22; 728 76); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x17372eb0&gt;&gt;
  2013-03-24 18:33:44.014 Bridge Alert[57793:1e503] Display:&lt;UILabel: 0xad3eaf0; frame = (20 20; 728 190); text = 'A bridge is considered “f...'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = &lt;CALayer: 0x17372eb0&gt;&gt;
  </code></pre>
  
  <p>As you can see, by Display, the label is resized. I'm assuming the height is recalculated somehow, based on if the cell were 320 pt wide, which is the built in UITableViewCell width.</p>
  
  <p>How can I get the label to size correctly?</p>