title: Autoresizing of UILabels in a UITableViewCell
body: <p>I am adding my own <code>UILabel</code>s to the <code>contentView</code> of a <code>UITableViewCell</code> because I need more control over the layout than the default UITableViewCellStyles provide.  In essence I want the detailLabel have priority over the textLabel so the textLabel gets truncated.</p>
  
  <p>I have the following code in my <code>UITableViewController</code>:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    static NSString * const kCellIdentifier = @"CustomCell";
  
    UITableViewCell * cell =
        [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
  
    UILabel * titleLabel, * dateLabel;
  
    if(cell == nil)
    {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
          reuseIdentifier:kCellIdentifier] autorelease];
  
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  
      titleLabel = [[[UILabel alloc] init] autorelease];
      titleLabel.tag = kTitleLabelTag;
      titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  
      dateLabel = [[[UILabel alloc] init] autorelease];
      dateLabel.tag = kDateLabelTag;
      dateLabel.textColor = [UIColor blueColor];
      dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  
      [cell.contentView addSubview:titleLabel];
      [cell.contentView addSubview:dateLabel];
    }
  
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
  }
  
  - (void)configureCell:(UITableViewCell *)pCell
    atIndexPath:(NSIndexPath *)pIndexPath
  {
    const float kHeight = 44.0, kLeftIndent = 8.0, kOverallWidth = 293.0,
        kGap = 1.0;
  
    UILabel * titleLabel, * dateLabel;
    titleLabel = (UILabel *)[pCell.contentView viewWithTag:kTitleLabelTag];
    dateLabel = (UILabel *)[pCell.contentView viewWithTag:kDateLabelTag];
  
    NSString * dateText = @"9:39 AM";
  
    // Calculate the size of dateLabel
    CGSize dateSize = [dateText sizeWithFont:[dateLabel font]
        constrainedToSize:CGSizeMake(kOverallWidth, kHeight)];
  
    const float dateXPos = kOverallWidth - dateSize.width;
    dateLabel.frame = CGRectMake(dateXPos, 0.0, dateSize.width, kHeight);
    titleLabel.frame = CGRectMake(kLeftIndent, 0.0,
        dateXPos - kLeftIndent - kGap, kHeight);
  
    titleLabel.text = @"Some potentially very long text which will be wrapped.";
    dateLabel.text = dateText;
    pCell.contentView.backgroundColor = [UIColor purpleColor];
  }
  </code></pre>
  
  <p>The code above produces incorrect results.  When the table view is initially shown, it looks like figure 1) in <a href="http://i.imgur.com/5jPc9.png">this image of the renderings</a>.</p>
  
  <p>So there is a <strong>unwanted gap on the right</strong> of all the <code>dateLabel</code>s. (the purple background is just for better visibility what is going on)</p>
  
  <p>When dragging the tableview up like in 2) in the image, it then bounces back and looks like 3).</p>
  
  <p><strong>The first row has now exactly the layout I wanted</strong> and which calculated in <code>configureCell:atIndexPath:</code>.  I guess this behavior happens because the cells get re-used and then configured again.</p>
  
  <p>So it feels like I am missing some kind of initialization, I have tried calling <code>setNeedsLayout</code> and <code>layoutSubviews</code> of <code>pCell</code> and <code>pCell.contentView</code> but never achieved an initial correct rendering.</p>
  
  <p>Only when I set the <code>autoresizingMask</code> of <code>titleLabel</code> and <code>dateLabel</code> to <code>UIViewAutoresizingNone</code> I get a correct initial rendering, then however the swipe to delete does not work because the delete button gets rendered over the <code>dateLabel</code>.</p>
  
  <p>What do I have to change in my code so that all cells get <em>rendered initially</em> like the first cell in the third picture?</p>
  
  <p>Thanks!</p>
  
  <p>PS: I would like to have inlined the pictures but unfortunately I do not have enough reputation for that.</p>