View-based NSOutlineView without NIB?

后端 未结 3 1562
难免孤独
难免孤独 2020-12-31 21:44

NSOutlineView is a subclass of NSTableView. And currently, NSTableView supports two implementations.

  • Cell-based.
3条回答
  •  一个人的身影
    2020-12-31 22:26

    Here's @jeberle's code re-written in Swift 4 (five years later!):

    class ProgrammaticTableCellView: NSTableCellView {
        override init(frame frameRect: NSRect) {
            super.init(frame: frameRect)
    
            self.autoresizingMask = .width
            let iv: NSImageView = NSImageView(frame: NSMakeRect(0, 6, 16, 16))
            let tf: NSTextField = NSTextField(frame: NSMakeRect(21, 6, 200, 14))
            let btn: NSButton = NSButton(frame: NSMakeRect(0, 3, 16, 16))
            iv.imageScaling = .scaleProportionallyUpOrDown
            iv.imageAlignment = .alignCenter
            tf.isBordered = false
            tf.drawsBackground = false
            btn.cell?.controlSize = .small
            // btn.bezelStyle = .inline                  // Deprecated?
            btn.cell?.isBezeled = true                   // Closest property I can find.
            // btn.cell?.setButtonType(.momentaryPushIn) // Deprecated?
            btn.setButtonType(.momentaryPushIn)
            btn.cell?.font = NSFont.boldSystemFont(ofSize: 10)
            btn.cell?.alignment = .center
    
            self.imageView = iv
            self.textField = tf
            self.addSubview(iv)
            self.addSubview(tf)
            self.addSubview(btn)
        }
    
        required init?(coder decoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        var button: NSButton {
            get {
                return self.subviews[2] as! NSButton
            }
        }
    }
    

    Edit: I found a link (that will inevitably rot away – it was last revised in 2011) to Apple's SidebarDemo that @jeberle based his code on.

提交回复
热议问题