That's an OSX app, XCode 9.2.
I've created a tableview in a window.
I've completely defined the tableView and its cell (standard) in nib. The NSTableCellView has an identifier ("NameCell")
The scrollView of the table is constrained to the leading and trailing (full window width minus 64)
When an instance of the window and tableView is created, I adapt width for some of them.
Problem is that the view redraws correctly but cellView.textField does not adapt its width (and so text is truncated even if there seems to be a lot of room available in the cell)
I could correct it with something that looks like a patch :
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if row < maxCount {
let p = dataSource[row]
if let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "NameCell"), owner: self) {
(cellView as! NSTableCellView).textField?.stringValue = p.name
// This the patch I had to add to make it work
(cellView as! NSTableCellView).textField?.frame.size.width = self.window!.frame.width - 64
return cellView
} else {
return nil
}
}
return nil
}
What do I miss in IB to have the textField automatically adapt its width to its container ?
The column has a fixed width (in size Inspector) that seems to force the cell width. I tried to constraint the column but could not do it.
It's the table view's responsibility to resize the cell ("cellView") to match the width of the table view. It's your responsibility to make sure subviews of the cell ("textField" in this example) are given a suitable size. Note that it's not generally desirable that it be linked to the cell size in all cases, just in this case.
You can use autolayout for this — link the leading and trailing edges of the text field to the leading and trailing edges of the cell. In the old days, before autolayout, if you wanted the text field to resize with the cell, you had to set the autoresizing controls for the same purpose. (In the latest Xcodes, you can still set the autoresizing controls appropriately in IB, and the autoresizing will be automatically translated into autolayout constraints.)