Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 814
Ely Avatar asked Oct 23 '17 13:10

Ely


2 Answers

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

like image 97
Gonche1124 Avatar answered Nov 08 '22 11:11

Gonche1124


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.

like image 44
Ely Avatar answered Nov 08 '22 10:11

Ely