Search code examples
macoscocoaautolayoutnstableview

Animate self-sizing NSTableView row height


I have a project that uses self-sizing table view cells. I'd like to animate when the height of a cell changes. NSTableView's noteHeightOfRows(withIndexesChanged:) will do this for view-based cells, but only when providing the height explicitly in the delegate's tableView(_, heightOfRow:) method. When cell height is calculated with auto layout, this value is ignored.

I can get the cell view itself to animate, but its containing row view jumps to the final size dictated by the constraints. Also tried using a height constraint on the row view and animating that, but it also was ignored. Anyone left developing Mac apps that has solved this problem?


Solution

  • I created a sample project and put it on Github, that animates the row height of a table. It does not use the delegates tableView(_, heightOfRow:) function. Take a look at it.

    The necessary steps:

    • Set the row height on the table view to use autolayout.
    • Surround animation with a call to beginUpdate() and endUpdate()on the table view.
    • Change properties of the controls in your NSTableCellView and call noteHeightOfRows(withIndexesChanged:) on the table within a call to NSAnimationContext.runAnimationGroup.

    However it is easier if you just look at the code I put on Github.