title: How to connect dataSource and delegate with code - iOS Swift
body: <p>I'm trying to have a better understanding on how the dataSource and delegate outlets get connected to the UITableView under the hood when you do the connection through the UI in Xcode by dragging and dropping to the viewController icon.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/27022136/set-delegate-and-datasource-of-uitableview-programmatically-swif">this thread</a> but I think I'm missing something because I cannot make it work.</p>
  
  <p>Here is the code I currently have that works fine by connecting the outlets through XCode (by drag and dropping).</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
  
      var hobbies:[String] = ["Computers", "Photography", "Cars", "Reading", "Learning New Things"]
  
  
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return 1
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return hobbies.count
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
          let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
  
          cell.textLabel?.text =  hobbies[indexPath.row]
  
          return cell
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>
  
  <p>I tried removing the outlet connections made by XCode, created an outlet for the tableView (<code>myTable</code>) and added the following code in the <code>viewDidLoad</code> method but it doesn't work, no error it just doesn't load the data.</p>
  
  <pre><code>        override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view, typically from a nib.
  
             myTable.delegate = self
             myTable.dataSource = self
          }
  </code></pre>
  
  <p>Can someone describe the steps needed to do this connection with code?</p>