使用NSGridView

时间:2018-01-28 19:27:00

标签: swift cocoa nstableview nsview nstablecellview

我尝试使用NSGridView来布局像这样的NSTableCellView

class GridItemCV: NSTableCellView
{ var grid :  NSGridView? = nil

  let empty = NSGridCell.emptyContentView

  var tf = NSTextField()

  override init(frame frameRect: NSRect)
  { super .init(frame: frameRect)
    setup()
  }

  required init?(coder decoder: NSCoder)
  { super .init(coder: decoder)
    setup()
  }

  func setup()
  { self.translatesAutoresizingMaskIntoConstraints = false
    tf.translatesAutoresizingMaskIntoConstraints = false

    grid = NSGridView( views: [
      [empty, empty],
      [empty, tf]
    ])
    grid?.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(grid!)

    grid?.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
    grid?.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    grid?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    grid?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
  }

我的问题。

应该由Interfacebuilder连接的属性textField尚未在构造函数中实例化。我什么时候可以访问这个属性?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

中构建NSGridView
awakeFromNib() 

我可以访问从Interfacebuilder

连接的属性textField