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.

Ely
  • 8,259
  • 1
  • 54
  • 67

2 Answers2

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.

Ely
  • 8,259
  • 1
  • 54
  • 67
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

Gonche1124
  • 41
  • 5
  • Thanks, but unfortunately this doesn't work in my code. Do you use `usesAutomaticRowHeights = true`? – Ely Nov 12 '17 at 16:59
  • 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? – Gonche1124 Nov 12 '17 at 19:01
  • 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? – Ely Nov 12 '17 at 21:13
  • 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. – Gonche1124 Nov 12 '17 at 21:33
  • OK, but then your are not really using the new automatic row heights feature. My code works without the `heightOfRow` delegate. – Ely Nov 12 '17 at 21:46
  • 1
    @Ely I was able to make it work without `heightOfRow ` or `tableViewColumnDidResize`, I followed [https://developer.apple.com/library/content/releasenotes/AppKit/RN-AppKit/#10_13NSTableView%20Automatic%20Row%20Heights] and made sure the layout constraints, hugging and compression were adequate – Gonche1124 Nov 18 '17 at 00:53