1

When you use default cell-based NSTableView and double click resizing spot in column, that column will resize to that width what is max longest text in your content.

What function I must call in view-based NSTableView to get same effect?

Edit: Function what I missed was:

func tableView(tableView: NSTableView, sizeToFitWidthOfColumn column:Int) -> CGFloat

But how can I get max width from my all stringValues?

Prontto
  • 1,671
  • 11
  • 21

1 Answers1

1

You can get the maximum width of all rows in a column like this:

var width: CGFloat = 0
for i in 0...(tableView.numberOfRows-1) {
    let view = tableView.viewAtColumn(column, row: i, makeIfNecessary: true) as! NSTableCellView
    let size = view.textField!.attributedStringValue.size()
    width = max(width, size.width)
}
return width+20 //add an optional padding
beeb
  • 1,187
  • 11
  • 32
  • Is there a more efficient code than this? It seems very slow compared to Finder app's behavior, for example, when `numberOfRows` value is very large. – alobaili May 21 '22 at 16:17
  • I found a faster implementation and posted it here: https://stackoverflow.com/a/72332567/10654098 – alobaili May 21 '22 at 19:21