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

我是研究僧i 提交于 2019-11-30 19:47:16

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/46890144/how-to-update-row-heights-of-nstableview-with-usesautomaticrowheights-true-aft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!