Xcode 10.1, Swift 4.2, macOS 10.14.2
I am trying to make a simple to do list app for macOS where there are a series of NSTableView
rows and inside each one is an NSTextField
. Each field is a to-do item. I want the NSTableView
rows to expand to fit the size of the text within each NSTextField
.
I have all of the following working:
- Setting the text in the
NSTextField
makes theNSTableView
row expand as needed. Auto layout constraints are set in my storyboard. - Using
tableView.reloadData(forRowIndexes: ..., columnIndexes: ...)
sets the text and resizes the table row correctly.But doing
tableView.reloadData()
always resets everyNSTextField
to a single line of text as shown here:Interestingly, if you click into the
NSTextField
after reloading the whole table, the field resizes to fit its content again:
I believe I have set all the appropriate auto layout constraints on my NSTextField
and I'm using a custom subclass for it as well (from a helpful answer here):
class FancyField: NSTextField{
override var intrinsicContentSize: NSSize {
// Guard the cell exists and wraps
guard let cell = self.cell, cell.wraps else {return super.intrinsicContentSize}
// Use intrinsic width to jibe with autolayout
let width = super.intrinsicContentSize.width
// Set the frame height to a reasonable number
self.frame.size.height = 150.0
// Calcuate height
let height = cell.cellSize(forBounds: self.frame).height
return NSMakeSize(width, height)
}
override func textDidChange(_ notification: Notification) {
super.textDidChange(notification)
super.invalidateIntrinsicContentSize()
}
}
⭐️Here is a sample project: https://d.pr/f/90CTEh
I'm at a loss as to what else I can try. Any ideas?