How to update row heights of NSTableView with usesAutomaticRowHeights = true after column resize?
Asked Answered
T

2

5

Since macOS 10.13 we can use NSTableView with automatic row heights, thanks to the new property usesAutomaticRowHeights and of course auto layout. This works quite nicely.

But when the user resizes a column, the calculated heights are no longer correct, and gaps appear in the tableview cells.

Is there a proven way to update the row heights after column resize in this scenario?

I already tried methods like updateConstraintsForSubtreeIfNeeded(), updateConstraints(), setNeedsDisplay(), reloadData() and so on, but nothing works.

Theodolite answered 23/10, 2017 at 13:7 Comment(0)
T
4

If you use NSTextField's in your design, make sure the Desired Width setting of each NSTextField with dynamic height, is set to Automatic. This setting is located in the Size Inspector.

Changing this resulted in automatic recalculation of the tableview row heights.

Theodolite answered 22/3, 2018 at 21:30 Comment(0)
M
3

Something that worked for me was to include the tableViewColumnDidResize notification method in the NSTableviewDelegate. There you can call the noteHeightOfRows method of the table view. Here is an implementation:

   //Recalculate when the screen moves
   func tableViewColumnDidResize(_ notification: Notification) {
     let allIndex = IndexSet(integersIn:0..<self.YOURTABLE.numberOfRows)
    YOURTABLE.noteHeightOfRows(withIndexesChanged: allIndex)
}

Here are the links to the documentation: tableViewColumnDidResize and noteHeightOfRows

Magnuson answered 12/11, 2017 at 16:3 Comment(6)
Thanks, but unfortunately this doesn't work in my code. Do you use usesAutomaticRowHeights = true?Theodolite
I have set the Row Size style to "Automatic (auto layout)" through Interface Builder. When you say it doesn´t work, do you mean it doesn´t get called or that it does nothing when being called?Magnuson
it does get called, but the height of the rows remain the same. All my other attempts were also in the same tableViewColumnDidResize delegate. Did you also implement the heightOfRow delegate? If true, remains your code working when your remove this delegate?Theodolite
Yes, I also have HeightOfRow implemented and it does not work if I remove this. What I´m doing there is creating and configuring the view and calling layoutSubtreeIfNeeded. After that, I return the height of the view.Magnuson
OK, but then your are not really using the new automatic row heights feature. My code works without the heightOfRow delegate.Theodolite
@Theodolite I was able to make it work without heightOfRow or tableViewColumnDidResize, I followed [developer.apple.com/library/content/releasenotes/AppKit/… and made sure the layout constraints, hugging and compression were adequateMagnuson

© 2022 - 2024 — McMap. All rights reserved.