How programmatically create a TableView

I want to know how programmatically create a TableView.

let tableView = NSTableView()

The main problem is: how can I set the number of columns?

I tried to create tableView programmatically many time,
But still can’t show the tableView on my screen.
Is anyone can help me?

...
let TV = NSTableView (frame: frame)
TV.setDataSource (self)
TV.setDelegate (self)

let W = CGFloat (256.0)

for x in 0...1 {
    let col = NSTableColumn (identifier: "\(x)")
    col.minWidth = W
    TV.addTableColumn (col)
}
...

thank you ibex10
you probably misunderstand me.
sorry, My English is poor,
ok, this way, how can I create a tableView with 2 columns programmatically?

Your English is good. It is just that I did not read your original post carefully.

I have corrected my previous post, which now shows how to create a table view with two columns.

thank you so much, ibex10
but how do I add a table cell view in a column?
I need populate data in the tableview which i just created programmatically, but i don’t know how.

You can use the following example as a starting point, but you should ideally read Apple-supplied resources on table view programming topics to learn how to do it on your own.

//
//  AppDelegate.swift

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate {

    @IBOutlet weak var window: NSWindow!
    @IBOutlet weak var scrollView: NSScrollView!


    func applicationDidFinishLaunching (aNotification: NSNotification) {
        // Insert code here to initialize your application
        
        let frame = scrollView.bounds

        let TV = NSTableView (frame: frame)
        TV.setDataSource (self)
        TV.setDelegate (self)
        
        let W = CGFloat (256.0)
        
        for x in 0...9 {
            let col = NSTableColumn (identifier: "\(x)")
            col.minWidth = W
            TV.addTableColumn (col)
        }
        
        scrollView.documentView = TV
        scrollView.hasHorizontalScroller = true
        scrollView.hasVerticalScroller = true
    }

    func applicationWillTerminate (aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func numberOfRowsInTableView (tableView: NSTableView) -> Int {
        print ("numberOfRowsInTableView:")
        return 100
    }
    
    func tableView (tableView: NSTableView,
        viewForTableColumn tableColumn: NSTableColumn?,
        row: Int) -> NSView? {
            print ("tableView:viewForTableColumn:row: \(row)-\(tableColumn!.identifier)")
            let v = NSButton ()
            v.title = "Jibber Jabber: \(row)-\(tableColumn!.identifier)"
            return v
    }
}

thank you, ibex10, my English is poor, so, you know… ,
any way, you help me a lot.
there is a last question:D .
your example make the NSButton on the table, i want to know how to make the NSTableCellView with data on the table.

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let tfv = NSTextField()
    tfv.integerValue = 4
    let tcv = NSTableCellView()
    tcv.textField = tfv

    return tcv
}

[/code]

this is my try, i want to create a tablecellView inside the table view. no success. why? (the other code is the same to your example)