I need a little help with NSTableView and dynamic row height
What I have:
A view-based single column NSTableVIew is associated with an array controller. Each NSTableCellView contains three subheadings: NSImageView, NSTextField (single line) and NSTextField (multi-line). Basically, this is the chat interface, so you will have a list of messages, senders and avatars.
What I want to achieve:
When the text is longer than the minimum line height, the line expands to fit the content. Like iMessage, bubbles expand to fit the message.
... which seems very natural, but of all the relevant solutions that I found on the Internet ( Ref 1 , Ref 2 ), none of which work for me.
Ref 2 looks fantastically written, but none of this applies to my application, as the sample project uses a third-party automatic code layout, and all this is for iOS. Ref 1 gives a very promising solution written in English. I tried to configure it using a "dummy view" as described in the solution, but could not correctly change and measure the height.
Here is my code for tableView:heightOfRow: _samplingView is a dummy view that has working limitations and is identical to that in tableView .
- (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;
Result: all row heights remain unchanged, so when I change the width of the _samplingView , the edge size does not change messageCell . I thought auto-layout would take care of this compression / expansion and let me measure the height. In fact, I am very confused.
Edit: for reference, it looks like this:
+-----------+---------------------------------------------------+ | | NSTextField | |NSImageView| sender | | avatar +---------------------------------------------------+ | | | | | NSTextField (multiline) | +-----------| message | | | | | | (high compression/hugging priority) | | | (this view should decide the height of row) | | | | +-----------+---------------------------------------------------+