Abin
Abin

Reputation: 550

Load data to NSTableView in swift

I am new to IOS/IOX development. I tried many sites to load data to my table on OSX swift using NSTableView. But all was failure. The procedure i did was refering View-Based NSTableView in Swift - How to

  1. Drag and droped a NSTableView, which was perfectly seen when I run the code.
  2. Selected the first column and gave Identifier as "List" after setting number of columns to 1
  3. In the appdelegate.swift I pasted as

    class AppDelegate: NSObject, NSApplicationDelegate,NSTableViewDataSource,NSTableViewDelegate {
    
        func applicationDidFinishLaunching(aNotification: NSNotification?) {
    
        }
    
        func applicationWillTerminate(aNotification: NSNotification?) {
    
        }
    
        func numberOfRowsInTableView(aTableView: NSTableView!) -> Int {
    
            println("reached 1")
            return 10
        }
    
        func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView {
    
            println("reached 2")
    
            var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    
            cell.textField.stringValue = "Hey, this is a cell"
    
            return cell
    
        }
    }
    
  4. Then i tried to Set the delegate and datasource as the Appdelegate for the table view by selecting table and pointing to "App Delegate" in "Application Scene". But it was not linking

  5. When i run the program , table with no cells was loaded

Any help is greatly appreciated.

Upvotes: 3

Views: 2011

Answers (3)

Denys Stas
Denys Stas

Reputation: 161

In your case, when using storyboards, you should make your view controller a data source and delegate for your table view, because they're contained in one scene. The class itself is called ViewController in Xcode project templates.

So, just move the code from application delegate to view controller and connect all the things in storyboard.

Upvotes: 0

Abin
Abin

Reputation: 550

 func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
    if tableView.identifier=="tableViewIdentifier" {

           let cellView = tableView.makeViewWithIdentifier("List", owner: self) as! NSTableCellView
            cellView.textField?.stringValue = "Hey, this is a cell"
            return cellView
        }
     return nil
    }

set identifier to tableview of dropped table also set identifier to table column, set the delegate and datasource to the view controller in which the table is contained.

Upvotes: 0

Daniel
Daniel

Reputation: 436

The easiest way would be selecting your table view in the XIB file and then in the Utilities panel on the right chose the third icon from the right (the arrow pointing right). Also make sure you show the document outline by clicking the icon in the lower left of the Interface Builder window. From the Utilities panel you can just drag from the delegate and datasource circle to your AppDelegate in the document outline.

Upvotes: 0

Related Questions