0

Situation I want to create a News-Article table within a macOS Cocoa application. (see image below) Each Cell consists of two labels. One for the header and one for the main-content.

Problem Now the length of the news-bodies and the width of the whole TableView is variable. I want to have a variable height on each Row/Cell depending on the size of the labels in it.

What we tried We tried the following: We have a TableView

 -> TableColumn
  -> Custom View
   -> LabelHeader
   -> LabelBody

The RowSizeStyle Property on TableView is set to "Automatic"

The cells are populated with a custom class. Basically we tried to implement this StackOverflow solution: Link to Solution -> which did not work for us.

(And many more solutions we could not get to run)

Question Can anyone provide a working tutorial or solution for this problem? Or at least a promising approach?

PS I use XCode 9.4.1 and Swift 4 on macOS 10.13.5

Any help gladly appreciated, Thank you!

img

Luchspeter
  • 744
  • 7
  • 24

1 Answers1

1

In a modern cocoa application you would use NSLayoutConstraint to implement dynamic height of a table view cell. The tricky part would be to get the height of the text that you can set the height of the constrain. This could be done being using an NSTextView instead of NSTextField and asking the layout manager for the current height.

NSRect usedRect = [[textView layoutManager] usedRectForTextContainer:[self textContainer]];
float newHeight = usedRect.size.height;

I used this in an objective c application some years ago so if you have further questions please let me know.

Marc T.
  • 5,090
  • 1
  • 23
  • 40
  • Thank you very much for your answer! I will try your suggestion and probably come back with questions. – Luchspeter Jul 04 '18 at 20:57
  • Thank you Marc! We managed to succeed with a combination of your help and this Stackoverflow Post: [link](https://stackoverflow.com/a/39920958/7274172) – Luchspeter Jul 05 '18 at 14:42