Search code examples
swiftmacosnstableviewnstablecellview

How do I set the width of NSTextField inside NSTableColumn


I have an NSTableView with 2 columns where the cells are plain NSTableCellViews and the text of its text field is set from a dictionary.

It is all created in IB where I just drag/dropped a Table view into my app.

My problem is that the cell text is truncated regardless of the width of the column. It does not matter if I set the column width programatically or if I resize it during runtime.

Here is my viewForTableColumn:

  func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
  {
    let cell : NSTableCellView = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! NSTableCellView
    cell.textField!.stringValue = dataArray[row].objectForKey(tableColumn!.identifier)! as! String
    return cell
  }

Here you can see the result (I'm listing installed apps along with their current version)

This is the output .

As you can see some of the rows with longer names are truncated.

Any pointers on how I can give the text fields the width they need to display all its content ? Either in Xcode/IB or programatically. This is my first OSX app so talk to me like I'm a Windows user ... ;)


Solution

  • You could use constraints. First select your text field and then press that small button down right where tooltip says "Resolve autolayout issues". Then select Add missing constraints.

    enter image description here