title: View-based NSTableView, make row height dependent on content
body: <p>I need a little help with NSTableView and dynamic row height</p>
  
  <p><em>What I have:</em></p>
  
  <p>A single-column view-based NSTableVIew bound to an array controller. Each NSTableCellView contains three subviews: NSImageView, NSTextField (single line) and NSTextField (multiline). Basically, this is a chat interface, so you would have a list of messages, senders and avatars.</p>
  
  <p><em>What I want to achieve:</em></p>
  
  <p>When text is longer than minimum height of the row, the row expands to fit content. Much like iMessage, the bubbles expand to fit message. </p>
  
  <p>...which seems like a very natural thing to do, but out of all the relevant solutions I found online, (<a href="https://stackoverflow.com/questions/7504546/view-based-nstableview-with-rows-that-have-dynamic-heights">Ref 1</a>, <a href="https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930">Ref 2</a>), none of which works for me. </p>
  
  <p>Ref 2 looks fantastically written but none of that applies to my application since the example project uses third-party auto layout code and the entire thing is designed for iOS. Ref 1 gave a very promising solution written in, well, English. I tried to set it up using a "dummy view" like described in the solution, but failed to even properly change and measure the height.</p>
  
  <p>Here's my code for <code>tableView:heightOfRow:</code>, <code>_samplingView</code> is the dummy view, which has working constraints and is identical to the one in <code>tableView</code>.</p>
  
  <pre><code>- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
  {
      NSTextField *textField;
      NSTextFieldCell *messageCell;
      for (NSView *subview in [_samplingView subviews]) {
          if ([[subview identifier] isEqualToString:@"message"]) {
              textField = (NSTextField*)subview;
              messageCell = ((NSTextField*)subview).cell;
          }
      }
      Message *message = [[_messagesArrayController arrangedObjects] objectAtIndex:row];
      _samplingView.objectValue = message;
  
      CGFloat width = [[[tableView tableColumns] objectAtIndex:1] width];
      [_samplingView setBounds:NSMakeRect(0, 0, width, CGFLOAT_MAX)];
      [_samplingView display];
  
      CGFloat optimalHeight = 10 + [messageCell cellSize].height; //messageCell's size stays the same when I change samplingView to try to measure the height
      return optimalHeight;
  
  }
  </code></pre>
  
  <p>Result: all row heights remain the same, somehow when I change the width of <code>_samplingView</code>, it doesn't re-size <code>messageCell</code>'s size. I thought auto-layout would take care of this compression/expansion and allow me to measure the height. Indeed, I am very confused. </p>
  
  <p>Edit: for reference, this is what my view looks like</p>
  
  <pre><code>     +-----------+---------------------------------------------------+
       |           |  NSTextField                                      |
       |NSImageView|  sender                                           |
       |  avatar   +---------------------------------------------------+
       |           |                                                   |
       |           |  NSTextField (multiline)                          |
       +-----------|  message                                          |
       |           |                                                   |
       |           |  (high compression/hugging priority)              |
       |           |  (this view should decide the height of row)      |
       |           |                                                   |
       +-----------+---------------------------------------------------+
  </code></pre>