Load data into NSTableView in swift mode

I am new to IOS / IOX development. I tried many sites for loading data into table on OSX swift using NSTableView. But it was all a failure. The procedure I did was referring to a View based NSTableView in Swift - How

  • Drag and drop the NSTableView that was perfectly visible when running the code.
  • Selected the first column and gave the id "List" after setting the number of columns to 1
  • In 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
    
        }
    }
    
          

  • Then I tried to set the delegate and datasource as Appdelegate for the table view by selecting the table and pointing to "App Delegate" in "Application Scene". But this is not related to

  • When I run the program, a table with no cells was loaded

Any help is greatly appreciated.

+3


source to share


3 answers


 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 id in the tableview of the dropped table also set id in the table column, set the delegate and data source to the view controller that contains the table.

0


source


The easiest way is to select the table view in the XIB file, and then, in the Utilities pane on the right, select the third icon from the right (arrow pointing to the right). Also make sure you show the document outline by clicking the icon in the lower left corner of the Interface Builder window. In the Utilities panel, you can simply drag from the circle of the delegate and data source to the AppDelegate in the document path.



0


source


In your case, when using a storyboard, you have to make your view controller the data source and delegate to the table view because they are contained in the same scene. The class itself is named ViewController

in the Xcode project templates.

So, just move the code out of the app delegate to view the controller and connect all the things in the storyboard.

0


source







All Articles