title: UITableViewCell Can Not Constrain Label to Right Edge
body: <p>I have subclassed <code>UITableViewCell</code> (with a xib), and I have noticed that when I use autolayout/constraints, the labels that I place in Interface Builder do not show correctly at runtime.</p>
  
  <p>First, I put three labels aligned left, center, and right with respect to the cell's content view.</p>
  
  <p>When I run my app, The left-most label is on the left, <strong>the center label is on the right</strong>, and the right-most label is nowhere to be seen.</p>
  
  <p>I logged the frame of the cell's content view at my <code>-tableView:cellForRowAtIndexPath:</code> (inside the <code>UIViewController</code> subclass that hosts the table view), and I get this value:</p>
  
  <p>(0.0, 0.0, <strong>600.0</strong>, 43.5)</p>
  
  <p>The width is reported to be 600, but I assume this is the generic, 'any device' storyboard size (even though, weirdly, the cell comes from a separated xib with only the cell view, not a storyboard or the xib of a view controller), and it will be sized accordingly by the table view after being passed to it (AS a side note, notice the height being 43.5, <em><strong>not</strong></em> 44!). </p>
  
  <p>I have tried several things, and found out the following:</p>
  
  <ol>
  <li>If I set constraints on my label against the leading (left), top and bottom edges of the cell, it displays appropriately.</li>
  <li>If I place the label at the right-most edge of the cell in Interface Builder, <strong>without any constraints</strong>, it shows where I placed it (but of course, on rotation to landscape, it will stay put a fixed distance from the left edge instead of sticking to the right edge).</li>
  <li>If I place the label anywhere, and add a constraint to the trailing (right) edge of the container, it is displayed or not depending on how much distance the constraint specifies. That is, the constraint seems to be stuck to the cell's original right edge of +600. If I place it near the left-most edge, but drag the constrain towards the trailing edge, it will appear somewhere on the right.</li>
  </ol>
  
  <p>I tried logging the label's frame from within the cell's code, like this:</p>
  
  <pre><code>override func layoutSubviews()
  {
      super.layoutSubviews()
  
      // (...label should be at the right position now?)
  
      println("Label frame: \(rightLabel.frame)") 
  }
  </code></pre>
  
  <p>On Interface Builder, the label is 41 x 21 and is positioned at (271, 11).
  The log above is called twice for one row, and gives:</p>
  
  <pre><code>Label frame: (271.0, 11.0, 41.0, 21.0)
  Label frame: (544.0, 11.0, 41.0, 21.0)
  </code></pre>
  
  <p>If, instead, I remove the trailing constraint and replace it by a leading constraint to the left edge of the content view, and leave the label on the same position in Interface Builder, the above logs give:</p>
  
  <pre><code>Label frame: (271.0, 11.0, 41.0, 21.0)
  Label frame: (278.0, 11.0, 41.0, 21.0)
  </code></pre>
  
  <p>So, What is going on with trailing constraints on table view cells? How do I align a label to the right edge??</p>
  
  <p><strong>EDIT:</strong> I added the following line to the cell's <code>layoutSubviews()</code> method:</p>
  
  <pre><code>println("Own frame: \(self.frame)")
  </code></pre>
  
  <p>And I get this output:</p>
  
  <pre><code>Label frame: (271.0, 11.0, 41.0, 21.0)
  Own frame: (0.0, 0.0, 600.0, 44.0)
  Label frame: (544.0, 11.0, 41.0, 21.0)
  Own frame: (0.0, 0.0, 600.0, 44.0)
  </code></pre>
  
  <p>So, the cell stays at 600 points wide, even after being put into the table view!</p>
  
  <p><strong>EDIT 2:</strong>
  So, I found the problem and obviously it was somewhere else: The table itself is stuck at 600 points wide, <strong>because I did not constrain it to the view controller's view edges in Interface Builder</strong>. </p>
  
  <p><strong>Silly me!</strong> </p>
  
  <p>Once I pinned the table view's four edges to those of its parent view, the problem corrected itself.</p>
  
  <p>First time using table views in Interface Builder...</p>