Based on Corbin's answer (btw thanks for shedding light on this):
Swift 3, NSTableView based on auto-view view for macOS 10.11 (and higher)
My setup: I have an NSTableCellView that is laid out using Auto-Layout. It contains (among other elements) a multi-line NSTextField , which can contain up to two lines. Therefore, the height of the presentation of the entire cell depends on the height of this text box.
I am updating the message as a table to update the height in two cases:
1) When resizing a table:
func tableViewColumnDidResize(_ notification: Notification) { let allIndexes = IndexSet(integersIn: 0..<tableView.numberOfRows) tableView.noteHeightOfRows(withIndexesChanged: allIndexes) }
2) When changing the object of the data model:
tableView.noteHeightOfRows(withIndexesChanged: changedIndexes)
This will cause the table view to ask the delegate for the new row height.
func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
First, we need to get our model object for the row ( entity ) and the corresponding cell identifier. Then we check if we really created a view for this identifier. To do this, we must maintain a list with cell views for each identifier:
// We need to keep one cell view (per identifier) around fileprivate var savedTableCellViews = [String : NSTableCellView]()
If none are saved, we need to create (and cache) a new one. We update the cell view using our model object and tell it to reinstall everything based on the current table view width. You can use the fittingSize height as the new height.
JanApotheker Mar 17 '17 at 9:34 on 2017-03-17 09:34
source share