How to add tableview datasource and delegate to a separate class/object?

cocoa, macos, swift, xcode

Solution

In Interface Builder you would need to add an object from the object library to the document outline and then set its class to your custom class. Then you can connect the delegate and datasource to that object.

Problem

I'm trying to learn how to make a native MacOS X app with SWIFT. I've no background in using Objective-C and even Xcode for the moment. I achieved to do a simple app which answer to some button clicks and modify some labels. Now, I want to put some stuff in a table. I've put a NSTableView on my UI and figured out that I will have to implement a class which offer implementation for the NSTableViewDelegate and NSTableViewDataSource protocols. I think that it would be cleaner, for code organization and readability, to put all the stuff to control my TableView in a separate module of the main AppDelegate. Anyway, any tutorial I've seen so far is just focused on how to implement the protocols and put them all in the AppDelegate class. I've created a new SWIFT file and put the temporary following code inside it: ``` import Foundation import Cocoa class LogTableController: NSObject, NSTableViewDelegate, NSTableViewDataSource { @IBOutlet var tableView: NSTableView! var logTableData:[NSDictionary] = [] func numberOfRowsInTableView (tableView: NSTableView!) -> Int { return 0 } func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn!, row rowIndex: Int) -> AnyObject! { return "Hello" } } ``` But I'm unable to find how to proceed to make outlet beetween my TableView and my custom class or an instance of that class. Nothing allow me to do that in the InterfaceBuilder. I think I'm missing something, any answer on how to organize my code to not put all the stuff in the AppDelegate class would be appreciated. Thanks !

Original source